gpt4 book ai didi

c++ - 让程序重新开始并获得新的值(value)?

转载 作者:搜寻专家 更新时间:2023-10-31 01:55:01 27 4
gpt4 key购买 nike

我正在使用一个程序,该程序需要将一个值放入变量中并对其执行一些操作。问题是我希望程序重新开始并向用户询问新值以再次处理新值。

例如,请看这段代码,它需要一个数字作为评分等级。处理完成后,我希望程序要求新成绩(例如下一位学生)。

#include <iostream.h>

int main (int argc, const char * argv[])
{
int n;
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";

case 9:
break;
case 8: cout<< " A : Very Good ";
break;

case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;

default:
break;
}
return 0;
}

最佳答案

您需要的是 while loop

int main (int argc, const char * argv[])
{
int n;
while(1) {
cout<< " Please Enter your grade : " ;
cin>>n;
switch (n/10) {
case 10: cout<< " A+ : Great! ";

case 9:
case 8: cout<< " A : Very Good ";
break;

case 7: cout<< " B : Good " ;
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout<< " Failed ";
break;

default:
break;
}
cout<<"do you wish to continue?(y/n)";
cin>>some_declared_variable;
if (some_declared_variable == 'n')
break; //hopefully this will break the infinite loop
}
return 0;
}

关于c++ - 让程序重新开始并获得新的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8916784/

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