I've set up dozens of do-while loops and they work flawless. But today I must be completely dim. I just can't make my loop stop looping when I type in '0' in my consol.
char choice; // My variable
do
{
cout << "\tMeny choice : 1\n" ;
cout << "\tMeny choice : 2\n" ;
cout << "\tMeny choice : 3\n" ;
..............
cout << "\tQuit : 0\n" ;
cout << "\t__________________\n\n" ;
cout << "\tMake a choice: " ;
cin >> choice; // Give the value of user input to variable 'choice'
switch( choice ) // Use a switch to test the value
{
case 1 : // Execute this block
break;
case 2 : // Execute this block
break;
case 3 : // Execute this block
break;
.......
} //END switch
} //END do and check the variable choice condition
while( choice !=0 ); // If the variable is not set to '0', keep looping
choice is a char variable but you are comparing it to int 0. You should compare it to '0'. Similarly the cases in the switch are wrong, they also assume that choice is an int. Probably better to make choice an int.
cin >> choice; // Give the value of user input to variable 'choice'
switch( choice ) // Use a switch to test the value
{
case'1' : // Execute this block
break;
case'2' : // Execute this block
break;
case'3' : // Execute this block
break;
.......
} //END switch
} //END do and check the variable choice condition
while( choice != '0' ); // If the variable is not set to '0', keep looping
cin >> choice; // Give the value of user input to variable 'choice'
choice -= '0';
switch( choice ) // Use a switch to test the value
{
case 1 : // Execute this block
break;
case 2 : // Execute this block
break;
case 3 : // Execute this block
break;
.......
} //END switch
} //END do and check the variable choice condition
while( choice !=0 ); // If the variable is not set to '0', keep looping
I want the user to be able to use letters and numbers, an 'int' will make it to complicated. I just forgot the ' ' around my numbers. Not the first time ;)
But an other problem just accured... When I puts in several letters/numbers in the consol, the loop keeps repeating the same amount of times.
For example: When I type '112233' in the consol and press enter, case one gets executed twise, then case two gets executed two times and then block three gets executed twise...
If I have a submeny in one of my cases, and someone type in '11' in my headmeny, will the consol enter the submeny case one under case one?
If the user enters '11' by mistake, will this put him/her in the // submeny switch1-case 1: something without showing the meny inside case 1: // submeny switch 1 ?