Repeat do while loop if switch statement reaches default

I have a while loop requiring user input. Inside this while loop is a switch statement. How can I do this so that if the default value is met, repeat the loop asking for the user gender again?

do { cout << "What is your weight?" << endl; cin >> weight; cout << "What is your height?" << endl; cin >> height; cout << "What is your age?" << endl; cin >> age; cout << "What is your gender?" << endl; cin >> gender; switch (gender) { case 'M': case 'm': cout << endl << Male(weight, height, age); break; case 'F': case 'f': cout << endl << Female(weight, height, age); break; default: cout << "What is your gender?"; } cout << "Do you want to continue? (Y/N)" << endl; cin >> stopApp; } while(toupper(stopApp) == 'Y'); 
+4
source share
4 answers

One option is to set a boolean, and if the default case is set, set it to true to repeat.

 bool repeat; do { repeat = false; //switch statement switch { default: repeat = true; } while(repeat); 

You can use the replay accordingly to find out which question you would like to repeat.

+5
source

A typical template for this kind of thing is a loop until the user enters the correct input. IE

  do { cout << "What is your weight?" << endl; cin >> weight; cout << "What is your height?" << endl; cin >> height; cout << "What is your age?" << endl; cin >> age; cout << "What is your gender?" << endl; cin >> gender; } while (!validGender(gender)); // process valid input 

While this does not exactly answer your question, this is a good example of what you are trying to do.

+4
source

Thanks to the break overload, which means β€œend of switch case” and β€œexit loop”, this is one of those unusual times when goto comes up.

 do { again: cout << "What is your weight?" << endl; cin >> weight; cout << "What is your height?" << endl; cin >> height; cout << "What is your age?" << endl; cin >> age; cout << "What is your gender?" << endl; cin >> gender; switch (gender) { case 'M': case 'm': cout << endl << Male(weight, height, age); break; case 'F': case 'f': cout << endl << Female(weight, height, age); break; default: cout << "What is your gender?"; goto again; } cout << "Do you want to continue? (Y/N)" << endl; cin >> stopApp; } while(toupper(stopApp) == 'Y'); 

FYI "man" and "woman" are not the only options for gender . Depending on your larger goals, I would recommend that you do not ask this question at all, allow the user to provide an arbitrary phrase in response, or, if this is a medical application in which the biological sex is factual important information, ask instead (and again allow the provision of an arbitrary phrase , because it is also not binary).

+1
source
 do { cout << "What is your weight?" << endl; cin >> weight; cout << "What is your height?" << endl; cin >> height; cout << "What is your age?" << endl; cin >> age; bool repeat(true); do { cout << "What is your gender?" << endl; cin >> gender; switch (gender) { case 'M': case 'm': cout << endl << Male(weight, height, age); repeat = false; break; case 'F': case 'f': cout << endl << Female(weight, height, age); repeat = false; break; default: break; } } while(repeat) cout << "Do you want to continue? (Y/N)" << endl; cin >> stopApp; } while(toupper(stopApp) == 'Y'); 
+1
source

Source: https://habr.com/ru/post/1502889/


All Articles