C ++输入时,程序结束而不继续

时间:2013-03-01 20:25:54

标签: c++

编辑:这是If-Else。请看看发生了什么。如果答案错了,有人可以告诉我如何返回吗?就像,不正确的回答,再次进入?

使用namespace std;

int main()
{

cout<<"Welcome to the Grade Database. Please insert your domain: " ;
cout<<"\n";
int d, n;
cin>>d;
cout<<"Now enter your total grade(between 0-100): " ;
cin>>n;
if (n>0 && n<59){
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;}
else if (n<60 && n>=69){
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;}
else if (n>70 && n<=79){
    cout<<"Better than the average!";
        cout<<"D-"<<n<<"  ~"<<d ;}
else if (n>80 && n<=89){
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;}
else if (n>90 && n<=99){
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;}
else if(n==100){
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;}
else{
    cout<<"Invalid Entry.";}

return 0;

}

1 个答案:

答案 0 :(得分:5)

C ++中的

switch不支持范围或条件,只支持完全匹配。由于您有条件,请尝试使用ifelse,如下所示:

cin>>n;
if (n>0 && n<59) {
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;
}
else if (n>=60 && n<=69) {
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;
}
else if (n>=70 && n<=79) {
    cout<<"Better than the average!";
    cout<<"D-"<<n<<"  ~"<<d ;
}
else if (n>=80 && n<=89) {
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;
}
else if (n>=90 && n<=99) {
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;
}
else if (n==100) {
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;
}
else {
    cout<<"Invalid Entry.";
}

您可能还需要一些换行符。只需再次编写cout <<代码就不会开始新行,请查看std::endl