So when I put the limits of the integer that the switch case uses I keep getting a infinite number of the output of default for example:
int (num);
cout<<"Choose a number between 1-5"<<endl;
cin>>num;
while (num<1 || num>5)
{
switch (num)
{
case 1:
cout<<" Good"<<endl;
break;
case 2 :
cout<<" Okay"<<endl;
break;
case 3:
cout<<" Decent"<<endl;
break;
case 4:
cout<<" Nice Try"<<endl;
break;
case 5:
cout<<"Failed"<<endl;
break;
default
cout<<" Not Valid"<<endl;
break;
{
{
So for this example how would I make the user choose from 1-5 and if not repeat the loop in order for them to try again.
This will loop until valid number is given then test it in the switch case.
int num;
cout<<"Choose a number between 1-5"<<endl;
while(cin>>num && (num < 1 || num > 5));
switch (num)
{
... //for brevity
}
This will loop through the switch cases until a valid input is given.
int num;
bool keepLooping = true;
cout<<"Choose a number between 1-5"<<endl;
while(cin>>num && keepLooping)
{
keepLooping = false;
switch(num)
{
... //for brevity
default:
keepLooping = true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.