0
cout<<"What HTH level are you (1,2,3): ";

       cin>>hthLevel;
            
            
            switch(hthLevel){
                                    case 1:out<<"Hand to hand level= 1"<<endl;break;
                                    case 2:out<<"Hand to hand level= 2"<<endl;break;
                                    case 3:out<<"Hand to hand level= 3"<<endl;break;
                                    default: cout<<endl<<"Incorrect";
                                    }

Need to know how to loop this so if 1,2 or 3 is entered it continues but if an incorrect value is entered it loops and repeats the switch statement again untill the correct value is entered???????
please help

5
Contributors
10
Replies
13
Views
5 Years
Discussion Span
Last Post by Cross213
0

you have to use a do-while or while loop in this case. You could try:

while(true){
cout...
cin...
switch()
{
case
...
default:
cout<<endl<<"Incorrect";continue;
}
break;
}

Edited by teo236: n/a

0

I'm no expert in c++ but I think you mean cout not out.
Anyways, you are going to use a while loop.

cout<<"What HTH level are you (1,2,3): ";
cin>>hthLevel;
int x=0;
while (x==0)
{
switch(hthLevel){
                 case 1:
                       cout<<"Hand to hand level= 1"<<endl;x=1;break;
                 case 2:
                       cout<<"Hand to hand level= 2"<<endl;x=1;break;
                 case 3:
                       cout<<"Hand to hand level= 3"<<endl;x=1;break;
                 default: 
                       cout<<endl<<"Incorrect";
}
}
0

I'm no expert in c++ but I think you mean cout not out.
Anyways, you are going to use a while loop.

cout<<"What HTH level are you (1,2,3): ";
cin>>hthLevel;
int x=0;
while (x==0)
{
switch(hthLevel){
                 case 1:
                       cout<<"Hand to hand level= 1"<<endl;x=1;break;
                 case 2:
                       cout<<"Hand to hand level= 2"<<endl;x=1;break;
                 case 3:
                       cout<<"Hand to hand level= 3"<<endl;x=1;break;
                 default: 
                       cout<<endl<<"Incorrect";
}
}

This won't work because the minute you choose an option, it will display it and make x = 1, thus exiting the loop immediately.

0

You should have an option in your loop called exit, and when it is chosen, x should be incremented, as it will no longer equal 0, and thus allowing you you to exit the loop.

0

Um, I'm not sure why you quoted that, but isn't that what you wanted? If you choose a correct option the program continues, otherwise it loops. Maybe I misunderstood...

0

You should have an option in your loop called exit, and when it is chosen, x should be incremented, as it will no longer equal 0, and thus allowing you you to exit the loop.

That's what x is.

0

That's what x is.

Yes I understand but the way you did it will only allow you to enter ONE option, eliminating the purpose of a loop in the first place. Because when x becomes 1, it is of course no longer equal to 0 (which is the criteria of your loop) and will not continue looping, there won't even be a loop to begin with.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.