简体   繁体   中英

expected expression error in nested if statement c++

I keep getting an expected expression error in my nested if else statement. 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.

#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. Move the if 's into the corresponding else clauses.

    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. 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.

You should also use proper indentation, as it greatly helps to improve code readability and helps avoid errors like these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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