所有if语句都执行c ++

时间:2016-10-31 14:58:10

标签: c++ codeblocks

我正在制作一个让用户插入成绩的程序,程序会根据该成绩显示一条消息。

由于某种原因,if语句都在执行,我不知道为什么会这样。

这是我的代码:

#include <iostream>
#include <random>
using namespace std;

int main()
{
 string ques;
cout << "Hello would you like to insert a letter grade or a number grade?\n";
cin >> ques;

    if (ques=="letter")
    {
        char grade;
        cout <<"Enter your grade: ";
        cin >> grade;

        switch (grade)
        {
            case 'F' :
                cout << "You should start revising!"<<endl;
                break;
            case 'D':
                cout <<"You can do better!"<<endl;
                break;
            case 'C':
                cout<<"Improvement is possible!"<<endl;
                break;
            case 'B':
                cout <<"You`re getting there , keep it up!"<<endl;
                break;
            case 'A':
                cout <<"Congratulations! See?? Your hard work payed off!"<<endl;
                break;
        }
    return 0;

    }
    else
    {
        int nrGrade;
        cout <<"Enter your grade: ";
        cin >> nrGrade;

        if (nrGrade >=0 && nrGrade <= 59)
        {
            cout <<"Your grade is an F. You should start revising!"<<endl;
        }
        else (nrGrade >= 60 && nrGrade <= 69);
        {
        cout <<"Your grade is a D. I`m sure you can do better!"<<endl;
        }
        if (nrGrade >= 70 && nrGrade <=79)
        {
        cout <<"Your grade is a C. I`m sure you can improve!"<<endl;
        }
        else (nrGrade >= 80 && nrGrade <=89);
        {
        cout <<"Your grade is a B. You`re getting there , keep it up!"<<endl;
        }
        if (nrGrade >= 90 && nrGrade <= 100)
        {
            cout <<"Your grade is an A. Congratulations for your grade! Keep it up!"<<endl;
        }
        return 0;
    }
}

我正在使用codeblocks,因为我在visual studio中遇到了一些问题。任何人都知道为什么会发生这种情况?

2 个答案:

答案 0 :(得分:3)

if (nrGrade >=0 && nrGrade <= 59)
{
    cout <<"Your grade is an F. You should start revising!"<<endl;
}
else (nrGrade >= 60 && nrGrade <= 69);
{
    cout <<"Your grade is a D. I`m sure you can do better!"<<endl;
}

此处,部分(nrGrade >= 60 && nrGrade <= 69);被视为else语句的指令,因为尾随;,并且因为else不将条件作为参数。请记住if-statement的语法是:

if( condition )
    statement-true
else
    statement-false

当你试着写:

if( condition1 )
    statement-true
else( condition2 );
    statement-false

然后执行以下带有cout的块,因为它与您的if / else无关。

实际上,您的代码相当于:

if (nrGrade >=0 && nrGrade <= 59)
{
    cout <<"Your grade is an F. You should start revising!"<<endl;
}
else
{
    nrGrade >= 60 && nrGrade <= 69;
}
cout <<"Your grade is a D. I`m sure you can do better!"<<endl;

我认为你想要的逻辑更多:

if (nrGrade >=0 && nrGrade <= 59)
{
    cout <<"Your grade is an F. You should start revising!"<<endl;
}
else
{
    if (nrGrade >= 60 && nrGrade <= 69)
    {
        cout <<"Your grade is a D. I`m sure you can do better!"<<endl;
    }
}

可写:

if (nrGrade >=0 && nrGrade <= 59)
{
    cout <<"Your grade is an F. You should start revising!"<<endl;
}
else if (nrGrade >= 60 && nrGrade <= 69)
{
    cout <<"Your grade is a D. I`m sure you can do better!"<<endl;
}

此处,第二个if用作else的语句,使用以下语法形成伪关键字else if

if( condition1 )
    statement1
else if( condition2 )
    statement2
else
    default-statement

顺便说一下,当if-和else-blocks都以相同的指令结束时,你可能想要提取它。换句话说:

int main()
{
    // ...

    if (ques=="letter")
    {
        // ...
        return 0;
    }
    else
    {
        // ...
        return 0;
    }
}

应该是:

int main()
{
    // ...

    if (ques=="letter")
    {
        // ...
    }
    else
    {
        // ...
    }
    return 0;
}

答案 1 :(得分:0)

错误的if-else实现。

应该是

if(condition1) {
    //do something
}
else if (condition2) {
    //do something else
}
else
{
    //do other stuff
}

例如:

if (nrGrade >=0 && nrGrade < 60)
{...
}
else if (nrGrade >= 60 && nrGrade < 70)
{...
}
else if (nrGrade >= 70 && nrGrade < 80)
{...
}
else if (nrGrade >= 80 && nrGrade < 90)
{...
}
else if (nrGrade >= 90 && nrGrade <= 100)
{...
}
else
{
cout << "ERROR"
}

否则不需要输入,因此您正在编写的比较,后跟“;”,被视为指令。

最佳