If else trouble

I can't seem to find the errors in this code. It's saying I have an else without if statements, as well as expecting { after a on line 19, and expected ; before {token.


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
27
28
#include<iostream>
using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Input a variable for a: ";
    cin >> a;
    cout << endl << endl << "Input a variable for b: ";
    cin >> b;

    sum = a + b;

    cout << "the sum of a plus b is: " << sum;

    if a(sum>=50){

    cout << endl << endl << "The answer is greater than or equals to 50";

    }else(sum<=50){

    cout << endl << endl << "the answer is less than or equal to 50";

    }
}
Why is there an 'a' before the conditional brackets?

Have you made your else statement an else if or getting rid of the condition after the else?
Get rid of the random a before your if statement and the condition on the else statement.

Why is there an 'a' before the conditional brackets?

Have you made your else statement an else if or getting rid of the condition after the else?


What are conditional brackets? And that last sentence made no sense.
Opps I never noticed the a before the if statement. Also I can't find the one before the condition on the else, can you show me where it is?

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
27
28
#include<iostream>
using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Input a variable for a: ";
    cin >> a;
    cout << endl << endl << "Input a variable for b: ";
    cin >> b;

    sum = a + b;

    cout << "the sum of a plus b is: " << sum;

    if (sum>=50){

    cout << endl << endl << "The answer is greater than or equals to 50";

    }else(sum<=50){

    cout << endl << endl << "the answer is less than or equal to 50";

    }
}
I found the error. I had to put a ; after the conditions on the else statement. Working code is..

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
27
28
#include<iostream>
using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Input a variable for a: ";
    cin >> a;
    cout << endl << endl << "Input a variable for b: ";
    cin >> b;

    sum = a + b;

    cout << "the sum of a plus b is: " << sum;

    if (sum>=50){

    cout << endl << endl << "The answer is greater than or equals to 50";

    }else(sum<=50);{

    cout << endl << endl << "the answer is less than or equal to 50";

    }
}


is that normal? the guy in the turorial video didn't have that there, lol.
Last edited on
You can't have a condition after the else statement. Line 23 should just be:
} else {
The else statement executes when the previous if statement(s) are false, so having a condition there is pointless.
Topic archived. No new replies allowed.