gpt4 book ai didi

c++ - 如果 switch 语句达到默认值,则重复 do while 循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:31 24 4
gpt4 key购买 nike

我有一个 do while 循环请求用户输入。在这个 do 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');

最佳答案

一个选项是设置一个 bool 值,如果达到默认情况,则将其设置为 true 以重复。

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

您可以适本地使用 repeat 来知道您还想重复哪个问题。

关于c++ - 如果 switch 语句达到默认值,则重复 do while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18880021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com