2

I have written a C++ program to test compound inequalities/equalities.

When I run it in the IDE, it stops before it closes, but when I actually compile and run it as a executable, the console closes before I can read the output, even though I have specifically put a line at the end to pause it.

The code is included below:

#include <iostream>

using namespace std;

int main()
{
    int x;
    int y;
    int a;
    int b;
    int test1;
    int equality;
    int test2;
    bool et1;
    bool lt1;
    bool mt1;
    bool et2;
    bool lt2;
    bool mt2;
    bool t1;
    bool t2;

    cout << "What would you like to do? \n 1) Test two conditions are met \n 2) test if one condition is met out of two \n";
    cin >> equality;

    cout << "what would you like to test for the first pair of numbers? \n 1) if x == y \n 2) if x < y \n 3) if x > y \n";
    cin >> test1;
    cout << "what would you like to test for the second pair of numbers? \n 1) if x == y \n 2) if x < y \n 3) if x > y \n";
    cin >> test2;
    cout << "Choose the first number to the first inequality/equality \n";
    cin >> x;
    cout << "Choose the second number to the first inequality/equality \n";
    cin >> y;
    cout << "Choose the first number to the second inequality/equality \n";
    cin >> a;
    cout << "Choose the second number to the second inequality/equality \n";
    cin >> b;

    if (test1 == 1 && x == y){
        et1 = true;
    }
    else if (test1 == 2 && x < y) {
        lt1 = true;
    }
    else if (test1 == 3 && x > y) {
        mt1 = true;
    }
    else if (test2 == 1 && a == b) {
        et2 = true;
    }
    else if (test2 == 2 && a < b) {
        lt2 = true;
    }
    else if (test2 == 3 && a > b) {
        mt2 = true;
    }

    if (lt1 == true || et1 == true || mt1 == true) {
        t1 = true;
    }

    if (lt2 == true || et2 == true || mt2 == true) {
        t2 = true;
    }

    if (equality = 1 && t1 == true && t2 == true) {
        cout << "this compound and inequality is true";
    }
    else if (equality = 2 ) {
        if (t1 == true || t2 == true) {
            cout << "this compound or inequality is true";
        }
        cout << "this compound inequality is true";
    }

    std::cin.get();
    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
Nate
  • 21
  • 1
  • 2
    Most likely the same root cause as [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/) - there is likely an unread `'\n'` char in `cin`'s buffer that `get()` ends up reading, thus why it doesn't wait for input as expected. Call `cin.ignore()` after the last `cin >> ...` to discard that `'\n'` char. – Remy Lebeau Nov 02 '19 at 17:50
  • Per Remy Lebeau, and since this is “why?” and not “how?”: possible duplicate of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Davis Herring Nov 02 '19 at 18:15

1 Answers1

-2

You can include the header file "conio.h" and do a getch() call whenever you want a pause in your program

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
      cout<<"After printing this there will be a pause.Press any character to contine\n";
      getch();
      cout<<"Code ends\n";
      return(0);
}
Pari Raju
  • 25
  • 5