Archived

This topic is now archived and is closed to further replies.

The Orange Peanut

Could someone explain why this isnt working?

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites