If you press 'y' hundreds of times the program works fine..and if you press 't' hundres og times the program works fine..BUT after you've pressed 't' once and you press 'y' after that, then the program break down! why?.. I find it hard to understand.. please help.. and what's the solution, if I want to switch between pressing 'y' and 't' hundreds of times?
It is because of the while loop structure...if you press 'y' a bunch it will work, then when you press 't' it exits the 'y' loop and enters the 't' loop, then if you press something other then 't', if exits the t loop and leaves main().
Also, indent your code a little so it is easier to see...
Because you forgot to remember that the code runs sequentially.
Let me walk you through your code to see why it did not turn out the way you expect it to be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//This is your main
//All the values are assumed to be set within the menu() function
char choice='0';
while (choice=='0')
{
choice=menu(); //Any values entered aside from '0' will exit this loop.
}
while (choice=='y')
{
choice=menu(); //The program loops here when you enter 'y' lots of times.
}
while (choice=='t')
{
cout <<"If I press 'y' now it goes out, why?!"<<endl;
//Because the loop checks, if choice is 't'. Since you enter 'y', 'y' == 't' is false. So the loop
//exits.
choice=menu();
}
bool play = true;
int number = 0;
while(play)
{
cout << " Lets see how smart you are. Enter a number between 1 and 6, 0(zero) to exit.\n";
cin >> number;
switch(number)
{
case 1:
cout << "1? ok...\n";
break;
case 2:
cout << "hmm 2? not bad.\n";
break;
//case 3: case 4: case 5: case 6:
case 0:
cout << "bye!\n";
play = false;
break;
default:
cout << "FOOL! i said a number between 1 and 6, not: " << number << " geez...\n";
break;
}//end of switch
}//end of while