C ++,两个“ if”语句都在不满足要求的情况下执行

时间:2018-12-06 20:30:09

标签: c++

由于某种原因,两个if语句都被执行,即使它们都不满足要求。如果您知道如何解决此问题,请发表评论。我昨天开始编码,所以不要讨厌我的愚蠢错误。

int search;
int notes;
int code;

int main()
{

        cout << "Welcome User\n\n";
        cout << "What do you want to use?\n";
        cout << "Private notes 'notes'\n";
        cout << "Code 'code'\n";
        cin >> search;
        if (search == code)
        {
            cout << "Code\n";
            cout << "So you wanted to see my code, huh?\n
        }
        if (search == notes)
        {
            cout << "Notes\n";
            cout << "This is a test Software made by Anes, on the 6th 
            dezember of 2018\n";
            cout << "This is my 1st real programm I'm trying to build.\n";
        }


    return 0;
}

1 个答案:

答案 0 :(得分:3)

变量名称不是值。

您的变量为int,您正在输入单词。 将非整数读为整数会失败,因此之后变量仍为零-它们全为零。

如果要检查特定的字符串,请使用字符串。

int main()
{
    string entry;
    cout << "Whats the password?\n";
    cin >> entry;
    {
        string search;
        cout << "Welcome User\n\n";
        cout << "What do you want to use?\n";
        cout << "Private notes 'notes'\n";
        cout << "Code 'code'\n";
        cin >> search;
        if (search == "code")
        {
            cout << "Code\n";
            cout << "So you wanted to see my code, huh?\n
        }
        if (search == "notes")
        {
            cout << "Notes\n";
            cout << "This is a test Software made by Anes, on the 6th 
            dezember of 2018\n";
            cout << "This is my 1st real programm I'm trying to build.\n";
        }

    }
    return 0;
}