简体   繁体   English

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

[英]expected expression error in nested if statement c++

I keep getting an expected expression error in my nested if else statement.我在嵌套的 if else 语句中不断收到预期的表达式错误。 I don't understand what the problem is because I have other statements in the code that are formatted (as far as I can see) the exact same way that don't generate the error!我不明白问题是什么,因为我在代码中还有其他语句,它们的格式(据我所知)与不会产生错误的方式完全相同!

This is for an assignment in a first year c++ class that calculates bowling scores based on user input data.这是针对第一年 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";




}

Edit: I was wrong, here's the error:编辑:我错了,这是错误:

    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;
        }

The second else has no associated if before it.第二个else没有关联的if之前。 Move the if 's into the corresponding else clauses.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;
    }

However, the more serious problem here is the way you approach the task.但是,这里更严重的问题是您处理任务的方式。 There are way too many nested if statements for this task.此任务有太多嵌套的if语句。 You repeat yourself a few times on the conditions, whereas the proper way would be to only check the condition once and then branch off.你在条件上重复几次,而正确的方法是只检查条件一次,然后分支。

Also, statements like另外,像这样的语句

if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10)
    userin_1 = userin_1;

make no sense, you should instead check for the opposite and skip the else clause altogether.没有意义,您应该检查相反的内容并完全跳过else子句。

You should also use proper indentation, as it greatly helps to improve code readability and helps avoid errors like these.您还应该使用适当的缩进,因为它极大地有助于提高代码可读性并有助于避免此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM