嵌套if语句c ++中的预期表达式错误

时间:2016-03-07 19:12:21

标签: c++

我在嵌套的if else语句中不断得到预期的表达式错误。我不明白问题是什么,因为我在代码中有其他语句格式化(据我所见),与生成错误完全相同!

这是针对第一年c ++课程中的作业,该课程根据用户输入数据计算保龄球分数。

#include <iostream>

using namespace std;

int main() {

    // Set Variables
    int userin_1;
    int userin_2;
    int userin_3;
    int combined2_3;
    int score;


    // Query User

    cout << "Welcome to Josh's Bowling Score Calculator! " << "Please enter your three scores. "
    << "They should be between 0 and 10." << endl << endl;

    cin >> userin_1 >> userin_2 >> userin_3;

    // Verify input data

    if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10)
        userin_1 = userin_1;
        else cout << endl << "Input Error: You cannot score greater than 10 in one turn! :|" << endl;

    if (userin_1 >= 0 && userin_2 >=0 && userin_3 >=0)
        userin_1 = userin_1;
    else cout << endl << "Input Error: You cannot score less than 0 in one turn! :|" << endl;

    if (userin_1 == 10)
        userin_1 = userin_1;
        else if (userin_1 + userin_2 <= 10)
            cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! "
            << endl;

    if (userin_2 == 10)
        userin_2 = userin_2;
    else if (userin_2 + userin_3 <= 10)
        cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! "
        << endl;

    // Calculate Score

    combined2_3 = userin_2 + userin_3;

    if (userin_1 + userin_2 < 10)
    {
        score = userin_1 + userin_2;
        cout << "Because you scored less than 10 on your first two throws, your last score doesn't count :[ "
    << "Your score is: " << score << endl;
    }
        else
        {
        if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10)
        score = userin_1 + userin_2 + userin_3;
        cout << "Wowow 3 strikes! Your score is " << score << endl;
        }
            else
            {
            if (userin_1 == 10 && userin_2 == 10)
            score = userin_1 + userin_2 + userin_3;
            cout << "Two strikes! Your score is " << score << endl;
            }
                else
                {
                if (userin_1 == 10 && userin_2 + userin_3 >= 10)
                    cout << "It's not possible to score greater than 10 on your last two throws, unless your first throw is a strike :| " << endl;
                }
                    else
                    {
                        if (userin_1 == 10 && combined2_3 >= 10)
                            score = userin_1 + userin_2 + userin_3;
                        cout << "Nice, a strike! Your score is " << score << endl;
                    }
                        else
                        {
                            if (userin_1 == 0 && userin_2 == 0 && userin_3 == 0)
                                cout << "Donny, you're out of your element. All zeroes." << endl << endl;
                        }


    // Closing Statement

    cout << endl << "Thanks for bowling with me, hope you have a great day";




}

1 个答案:

答案 0 :(得分:0)

编辑:我错了,这是错误:

    else
    {
    if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10)
    score = userin_1 + userin_2 + userin_3;
    cout << "Wowow 3 strikes! Your score is " << score << endl;
    }
        else
        {
        if (userin_1 == 10 && userin_2 == 10)
        score = userin_1 + userin_2 + userin_3;
        cout << "Two strikes! Your score is " << score << endl;
        }

第二个else之前没有关联if。将if移到相应的else子句中。

    else if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) {
        score = userin_1 + userin_2 + userin_3;
        cout << "Wowow 3 strikes! Your score is " << score << endl;
    } else if (userin_1 == 10 && userin_2 == 10) {
        score = userin_1 + userin_2 + userin_3;
        cout << "Two strikes! Your score is " << score << endl;
    }

然而,这里更严重的问题是你接近任务的方式。此任务有太多嵌套if语句。你在条件上重复几次,而正确的方法是只检查一次然后分支。

此外,还有

之类的陈述
if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10)
    userin_1 = userin_1;

没有意义,你应该检查相反的情况并完全跳过else条款。

您还应该使用适当的缩进,因为它极大地有助于提高代码的可读性,并有助于避免这些错误。