Keeps saying error 'else wthout a previous if'

I know there is an if there and I can't find any mistakes in the code (although I'm sure there is) in my second project which is a simple math quiz. Someone please help me with how to get rid of the 'else without a previous if' error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main ()
{
    cout << "Welcome to Aidan's math quiz 1.0 \n";
    cout << "This will be an easy 10 question math quiz \n";
    cout << "Type play when you want to being :) \n";

    string operation;
    cin >> operation;
    if (operation == "play \n");
{
        cout << "Let's begin! \n";
}
    else
{
    cout << "We can't start unless you press play! \n";
}

    cout << "Question 1: \n";



    return 0;
}
Last edited on
I really dont understand what went wrong here.
yep ty for help
now I have a problem where when i type play it says let's begin and then all of question 1 and incorrect how do i stop this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 if (operation == "play"){
        cout << "Let's begin! \n";
    }

    else
{
    cout << "We can't start unless you  type play! \n";
}
    cout << "Question 1: \n";
    cout << "10 - 17 + 16 + (12*12)";

    if (operation == "153")
{
    cout << "Correct! \n";
}
    else
{
    cout << "Incorrect. Try again on Question 2! \n";
}



    return 0;
}
omg i feel so stupid ty so much for helping so much. sorry for bugging wish i just knew more although it's day 2 of c++
ugh everytime i feel like i am going to finish with no more errors something else happens i have no idea what to do because no knowledge of c++ pretty much.
1
2
3
4
5
6
7
8
9
10
  cout << "Question 2 \n";
    cout << "What is the six letter acronym for the order of operation? \n";

    cin >> operation;
    if (operation == "PEMDAS"){
        cout >> "Correct \n";
    }


    return 0;
it says line 39 : no match for 'operator>>' what do i do
Last edited on
line 39 is cout >> "correct \n"; btw

Last edited on
your code worked great except for when i answered question 1 wrong, then it just did nothing, it didn't bring up question 2. also what does string operation and answer mean?
wait i fixed it
1 more problem. when u type something else besides play it says the "we can't start etc" but then it still goes to question 1 is there a way to stop this if the user does not type play? here is my code
1
2
3
4
5
6
7
8
9
10
    string operation;
    string answer;
    cin >> operation;
    if (operation == "play"){
        cout << "Let's begin! \n";
}else{
    cout << "We can't start unless you press play! \n";
}
     cout << "Question 1: \n";
     cout << "10 - 17 + 16 + (12*12)"<<endl;
and don't worry about the function thing its fine
Last edited on
nvm i got it i did return 0;
i don't understand what's different about urs and mine but return 0; worked for me
urgh idk what is wrong with this i know i spelled pythagorean theorem right bec i copy and pasted into the compiler and it said incorrect. try Q5 etc..
1
2
3
4
5
6
7
8
9
10
11
 cout << "Question 4 \n";
    cout << "What is the theorem that says a squared + b squared = c squared? (Make sure to capitalize each word.)  \n";

    cin >> answer;
    if (answer == "Pythagorean Theorem") {
        cout << "Correct! \n";

}else{
        cout << "Incorrect! Try again on Question 5! \n";

    }
i dont understand the cin >> answer and the whole format has been working fine until now
1
2
3
4
5
6
7
8
9
10
11
cout << "Question # \n";
    cout << " QUESTION HERE \n";

    cin >> answer;
    if (answer == "ANSWER HERE") {
        cout << "Correct! \n";

}else{
        cout << "Incorrect! Try again on Question #! \n";

    }


this whole format has been working for every question until 4
Last edited on
that helped! just wanting to know why did cin >> answer; work before but not now?
ty! :D
Topic archived. No new replies allowed.