What is wrong with this code and why is the console windows closing?

#include <iostream>
using namespace std;
int main()
{ int X;
int O;
int x;
int o;
int selection;
cout<<"Please select your tic-tac-toe team. X or O\n";
cin>> selection;
cin.ignore();
if ( selection == x ) {
cout<<"You have chosen the X team!\n";
}

else if ( selection == X ) {
cout<<"You have chosen the X team\n";
}
else if ( selection == o ) {
cout<<"You have chosen the O team!\n";
}
else if ( selection == O ) {
cout<<"You have chosen the O team!\n";
} else {
cout<<"Bad input, please select another team.\n";
cout<<"Please select your tic-tac-toe team.\n";





cin>> selection;
cin.ignore();
}
if ( selection == x ) {
cout<<"You have chosen the X team!\n";
}

else if ( selection == X ) {
cout<<"You have chosen the X team\n";
}
else if ( selection == o ) {
cout<<"You have chosen the O team!\n";
}
else if ( selection == O ) {
cout<<"You have chosen the O team!\n";
} else {
cout<<"Bad input, please select another team.\n";
cout<<"Please select your tic-tac-toe team.\n";
}
cin.get();
}
You should define selection variable as a char not as an int. Also in your if statements you should have written if (selection == 'x') instead of if (selection == x) and that applies to the others as well. Furthermore, you don't have to declare x, X, O, o integers because they aren't needed.
Thanks a whole lot. So do you think the problem was that I did not put 'and' around them?
Also, strangely, when I listened to your instructions, the console window did not close.
Well, it seems that you have fixed my problem.
That was Wisely Done. :)

PS Wow, I guess I had it built into my mind that I need to declare them...
I guess the C++ language is not THAT complicated yet.
Last edited on
Topic archived. No new replies allowed.