doubt

Hi, im noob, how can I make the app close only if the user PREss X
Its closing pressing any keyboard input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <locale>
#include <iomanip>

using namespace std;

int main(void)
{
    setlocale(LC_ALL, "Portuguese");
    int Side;
    int Square;
    Side = 4;
    Square = Side * Side;
    char x,X = 'x';
    cout << Square << "\n\nThe result has appeared." << endl;
    cout << "\n\nNow press X to exit." << endl;
    cin >> x;
    if ( x = true )
    {
        cout << "\n\n  Exiting..." << endl;
    }
    else
    {
        cout << Square << "\n\nThe result has appeared." << endl;
    }
        cin.get();
        return (0);
}
In the function int main() you can use return 0; to exit. Alternatively you can use exit(); and in the parentheses either EXIT_SUCCESS or EXIT_FAILURE
Last edited on
The name of the variable has no relationship with its content.
1
2
3
char input;
std::cin>>input;
if( input=='x' )
@ne555

yes u fixed it thanks but if i press S for example it still closes the app how can i stop it from closing?
did it worked putting another cin.get ty everyone :D
Last edited on
Try putting system("pause"); just one line above return 0; and let me know if that fixes the problem. Some compilers exit automatically without waiting for user input.
Try using another key to reprint the result. For Example:
1
2
3
4
5
6
7
8
9
if(input == 'x'){
return 0;
}
else if (input == 'r'){
cout << Square << "\n\nThe result has appeared." << endl;
}
else {
cout << "That is Not A Valid Input";
}
Last edited on
Topic archived. No new replies allowed.