Mr.*_*1.0 4 c++ switch-statement do-while
我有一个while循环请求用户输入.在while while循环中我有一个switch语句.如何才能使其达到默认值,重复循环再次询问用户性别?
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');
Run Code Online (Sandbox Code Playgroud)
一种选择是设置一个布尔值,如果达到默认情况,则将其设置为true以重复.
bool repeat;
do {
repeat = false;
//switch statement
switch {
default:
repeat = true;
}
while(repeat);
Run Code Online (Sandbox Code Playgroud)
你可以适当地使用重复来知道你想重复哪个问题.