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

Recommended Answers

All 10 Replies

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;
}

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";
}
}

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.

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.

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...

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.

I think it will work......
use goto statement where you want to start.

Your question is not actually clear to me.The switch case automatically does this.
Please explain your problem clearly.

hmm, you can post a sample output that you want.It will clear the problem that you want to solve.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.