int MainMenu()
{
int choice = -1;
string strchoice;
while((choice != 0) || (choice != 1) || (choice != 2) || (choice != 3))
{
cout<<"\tSelect a play style."<<endl;
cout<<"\t1 = Single Player"<<endl;
cout<<"\t2 = Two Players"<<endl;
cout<<"\t3 = Demo"<<endl;
cout<<"\t";
getline(cin,strchoice, '\n');
if(strchoice=="1")
{
choice = 1;
}
}
system("cls");
return choice;
}
Thank you.
[edited by - The Orange Peanut on October 8, 2003 2:10:43 PM]
Could someone explain why this isnt working?
Im trying to write a menu for my tictactoe game. I should know how to do this (Im a newbie, but not so new that I cant handle string manipulation), but it is giving me trouble and I cant see what is wrong. The problem is that the loop never ends... even if I put it 1 as the input, the loop doesnt end and system("cls"); is never called. Here is my code
Your conditional in the while loop is wrong. When choice is equal to 1, it isn't equal to 0, or 2, or 3 and that will allow the loop to continue...
[edited by - stonicus on October 8, 2003 2:02:19 PM]
[edited by - stonicus on October 8, 2003 2:02:19 PM]
Okay, I fixed the problem, but I still dont see why the other way didnt work. The conditional as I seen it was "If strchoice != 1 OR 2 OR 3, then keep the loop going." Was it the way I wrote it or would that just not work?
[edited by - The Orange Peanut on October 8, 2003 2:12:03 PM]
[edited by - The Orange Peanut on October 8, 2003 2:12:03 PM]
Try this
void MainMenu(){ int choice=0; while ((choice != 1) || (choice != 2) || (choice != 3)); { cout << "\t1 Single Player"; cout << "\t2 Two Players"; cout << "\t3 Demo\t"; cin >> choice; } return choice;}
Try this instead
void MainMenu(){ char chrChoice; int choice = 0; while (choice == 0); { cout << "\t1 Single Player"; cout << "\t2 Two Players"; cout << "\t3 Demo\t"; cin >> chrChoice; switch (chrChoice) case ''1'': choice = 1; break; case ''2'': choice = 2; break; case ''3'': choice = 3; break; default: choice = 0; break; } return choice;}
What it was saying was:
if choice != 0 OR
if choice != 1 OR
if choice != 2 OR
if choice != 3...
keep going.
Well, if choice equals 1, it won''t equal 0 or 2 or 3... so three of those statements will be true.
If you want to say if choice != 0 or 1 or 2 or 3, you have to use different syntax. You want the loop to continue if they are ALL true, not if any of them are true.
if choice != 0 OR
if choice != 1 OR
if choice != 2 OR
if choice != 3...
keep going.
Well, if choice equals 1, it won''t equal 0 or 2 or 3... so three of those statements will be true.
If you want to say if choice != 0 or 1 or 2 or 3, you have to use different syntax. You want the loop to continue if they are ALL true, not if any of them are true.
quote:Original post by The Orange Peanut
Im trying to write a menu for my tictactoe game. I should know how to do this (Im a newbie, but not so new that I cant handle string manipulation), but it is giving me trouble and I cant see what is wrong. The problem is that the loop never ends... even if I put it 1 as the input, the loop doesnt end and system("cls"); is never called. Here is my code
int MainMenu(){ int choice = -1; string strchoice; while((choice != 0) || (choice != 1) || (choice != 2) || (choice != 3)) { cout<<"\tSelect a play style."<<endl; cout<<"\t1 = Single Player"<<endl; cout<<"\t2 = Two Players"<<endl; cout<<"\t3 = Demo"<<endl; cout<<"\t"; getline(cin,strchoice, ''\n''); if(strchoice=="1") { choice = 1; } } system("cls"); return choice;}
Thank you.
[edited by - The Orange Peanut on October 8, 2003 2:10:43 PM]
Man use a switch statement, so much easier...goto cprogramming.com for a tutorial on switchs
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement