我对else if语句做错了什么

时间:2014-08-05 08:34:48

标签: c++ if-statement

程序告诉我它需要一个带有else if语句的语句。在我上大学之前,我参加任何CS课程之前,我真的很新,而且我正在尝试学习c ++代码。提前致谢!

#include <iostream>
#include <string>
using namespace std;
string color;
string Blue;
string Green;
string Brown;

int age;
int main()
{
    cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
    cin >> color;

    if (color == Blue); {
        cout << "you are an intelligent person " << endl;
        system("pause");
    }
    else if (color == Green); {
        cout << " you are a peaceful person " << endl;
        system("pause");
    }
    else if (color == Brown); {
        cout << "you are a normal go get 'em person " << endl;
        system("pause");
    }


    cin.ignore();
    cin.get();
    return 0;

}

6 个答案:

答案 0 :(得分:11)

你的问题是你在括号后面有分号。这样:

if (color == Blue); {
    cout << "you are an intelligent person " << endl;
    system("pause");
}

应该是这个

                 No semi colon here
                  v
if (color == Blue) {
    cout << "you are an intelligent person " << endl;
    system("pause");
}

对于其余的其他人来说也一样

另外,正如其他人所提到的,你需要指定蓝色,绿色和棕色。要么这样做:

const string Blue = "Blue";
const string Green = "Green";
const string Brown = "Brown";

或者如果你想:

 if (color == "Blue") {   //Note the ""
    cout << "you are an intelligent person " << endl;
    system("pause");
}

答案 1 :(得分:3)

您已将分号放在他们不属于的地方。您还需要为用于比较的字符串赋值:

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

int main()
{
string color;
string Blue = "Blue";
string Green = "Green";
string Brown = "Brown";

    int age;

    cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
    cin >> color;

    if (color == Blue)
    {
        cout << "you are an intelligent person " << endl;
        system("pause");
    }
    else if (color == Green)
    {
        cout << " you are a peaceful person " << endl;
        system("pause");
    }
    else if (color == Brown)
    {
        cout << "you are a normal go get 'em person " << endl;
        system("pause");
    }

    cin.ignore();
    cin.get();
    return 0;
}

答案 2 :(得分:2)

您需要在if和else if语句之后删除错误的分号,如下所示:

if (color == Blue) {
    cout << "you are an intelligent person " << endl;
    system("pause");
}
else if (color == Green) {
    cout << " you are a peaceful person " << endl;
    system("pause");
}
else if (color == Brown) {
    cout << "you are a normal go get 'em person " << endl;
    system("pause");
}

答案 3 :(得分:2)

在C ++中,您可以将;视为语句终止符

if (color == "Blue");之类的东西本身就是一种陈述。但是,由于;终止if语句,它无法执行任何操作。

此外,可以使用大括号{}对语句进行分组。实际上,这可以用于限制在大括号内声明的任何变量的范围。您将在适当的时候发现这对计划稳定性产生的积极影响。

简而言之,一旦你修复了狡猾的字符串比较(你需要使用文字的引号字符,就像我上面所做的那样),你的代码在语法上完全有效。这就是编译的原因。 (虽然一个好的编译器应该警告你关于空if控制块)。但是它没有做你想做的事情:你需要删除包含;的所有行中无关的if

答案 4 :(得分:0)

除了额外的分号外,您还没有实际初始化字符串值,因此它们都是空字符串。您需要在开头添加:

Blue = "Blue";
Green = "Green";
Brown = "Brown";

答案 5 :(得分:0)

您的变量BlueGreenBrown是空字符串变量。

string Blue; // empty string with name Blue
string Green; // empty string with name Green
string Brown; // empty string with name Brown

首先,您需要删除worng分号,并且需要使用包含内容的字符串。 您可以省略变量并使用字符串文字:

if (color == "Blue") {

或者您需要在字符串中填写一些数据:

std::string Blue("Blue");
// ...
if (color == Blue); {