Why this simple pass/fail in exam program is giving error?

Pages: 12
I am trying to compile this program but compiler gives an error "else without a previous if". Why?

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
29
#include <iostream>
using namespace std;
int main()
{
	int marks;

	cout << "Please enter your marks\n";
	cin >> marks;

	if (marks > 60)
	{
		cout << "you have passed the exam\n";
		if (marks > 80)
		{
			cout << "Excellent job\n";
		}
		else
		{
			cout << "Good job\n";
		}
	else
	{
		cout << "You are fail in this exam";
	}
	}

	
	return 0;
}
Last edited on
Mismatched opening/closing braces { }.

Lines 24,25 two closing braces where there should be one.
Before line 21 - closing brace missing - to match open brace at line 11.
Ok, now working.

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
29
30

#include <iostream>
using namespace std;
int main()
{
	int marks;

	cout << "Please enter your marks\n";
	cin >> marks;

	if (marks > 60)
	{
		cout << "you have passed the exam\n";
		if (marks > 80)
		{
			cout << "Excellent job\n";
		}
		else
		{
			cout << "Good job\n";
		}
	}
	else
	{
		cout << "You are fail in this exam";
	}

	
	return 0;
}


But, why compiler was giving me error because according to book "Problem Solving with C++ by Walter Savitch" in which author said "the compiler does not care about indenting" mean to compiler both program one with braces and other with without braces will work same (here is the link of image of topic https://i.imgsafe.org/ef88208.png ). But I am facing 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
27
[code]
#include <iostream>
using namespace std;
int main()
{
	int marks;

	cout << "Please enter your marks\n";
	cin >> marks;

	if (marks > 60)
	cout << "you have passed the exam\n";

	if (marks > 80)
        cout << "Excellent job\n";

	else
	cout << "Good job\n";
	
	
	else
        cout << "You are fail in this exam";
	

	
	return 0;
}


Why?
Last edited on
The indentation is the tabs (or spaces) that you put at the beginning of the line to align the code. It is true that this is ignored by the compiler. { and } is not part of the indentation and is not ignored by the compiler.
Last edited on
#peter Then why second program is not working?
The second program is like 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
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int main()
{
    int marks;

    cout << "Please enter your marks\n";
    cin >> marks;

    //---------------------------------------------------
    
    if (marks > 60)
        cout << "you have passed the exam\n";

    //---------------------------------------------------       
        
    if (marks > 80)
        cout << "Excellent job\n";
    else
        cout << "Good job\n";
   
    //---------------------------------------------------   
    
    else
        cout << "You are fail in this exam";
        
    //---------------------------------------------------

    
    return 0;
}


I used comment line to break up the code visually.
The else at line 24 doesn't match either of the previous ifs.
That is my question if "the compiler does not care about indenting" why then program not working?
It's not working because the last else statement doesn't have a if statement, which it needs to. You Cant have an else without an if. And you can't have 2 elses with 1 if. But you can have as many if elses you want, not elses though.

Works fine :
1
2
3
4
if
else if
else if
else


Doesn't work fine:

1
2
3
4
if
else if
else // if statement considered over here.
else  // so because of that this else is missing an if 


Your compiler tells you this -
error: 'else' without a previous 'if'
And it's kinda clear what it says.
Last edited on
#TariqNeaj here is problem https://i.imgsafe.org/ef88208.png
I am confused if the author is saying that "the compiler does not care about indenting" then why I have to write if else seperately. Here:-

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

	cout << "Please enter your marks\n";
	cin >> marks;

	if (marks > 60)
	cout << "you have passed the exam\n";

	if (marks > 80)
        cout << "Excellent job\n";

	else
	cout << "Good job\n";
	
	
	else
        cout << "You are fail in this exam";
	

	
	return 0;
}


If compiler don't care about spaces then why above program is wrong. As C++ automatically connects the else with last unconnected if. As in above program we can see that in mid if-else are connected then remaining is only one if.
Last edited on
Your first else connects with the if before it. But your last else connects with nothing.

Yes, it does not care about spaces or indentation, but it does care about what order things come. Once your second if statement starts, your first one ends, its gone, forever.
Last edited on
Because you have two "else" statements following the same "if".

It looks to me like what you really wanted was to have the second "if" inside a conditional block that starts at line 13. If you want more that one statement inside a block, you need braces, so:

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
#include <iostream>
using namespace std;
int main()
{
	int marks;

	cout << "Please enter your marks\n";
	cin >> marks;

	if (marks > 60)
	{
	cout << "you have passed the exam\n";

	if (marks > 80)
        cout << "Excellent job\n";

	else
	cout << "Good job\n";
	}
	
	else
        cout << "You are fail in this exam";
	

	
	return 0;
}


Nothing to do with indentation at all.
Last edited on
If compiler don't care about spaces then why above program is wrong.

It's not the spaces that is the problem. The problem is that you have an else without if.

C++ automatically connects the else with last unconnected if.

Well, not if there are other statements between the else and the last unconnected if.

1
2
3
4
5
6
7
8
9
10
if (marks > 60)
	cout << "you have passed the exam\n";

if (marks > 80)
	cout << "Excellent job\n";
else
	cout << "Good job\n";
	
else // This else can obviously not be "connected" with the if on line 1.
	cout << "You are fail in this exam";


I think the point that the author is trying to make is that when we write pseudocode the indentation has meaning to us humans, but when you translate the pseudocode into C++ you need to make sure to write the code so that it becomes valid C++ and that it does what you want it to do. You can no longer rely on the indentation for correct behaviour. To do this you often have to use curly brackets { }. Personally I always use curly brackets (except for else if) even if it's not needed.
Last edited on
Ok, now clear mean we can use spaces as much as we want but our program must have some order so compiler can understand it. Thanks #Peter87.
Yes. "Indentation" just means the whitespace you use. The syntax, on the other hand, is crucial, and includes the correct use of braces to identify your code blocks.

Thanks #MikeyBoy
Hi bro, I'm new at C++ and I read your thread, and just as everybody else said, indentation doesn't mean that you can use extra { or }, and if you do then you need to have an opening and a closing one.

Also, I'm sure that you fixed your code by now, but I also rewrote your code and maybe you can try this out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	int marks;

	std::cout << "Please enter your marks: ";
	std::cin >> marks;

	if (marks >= 60 && marks < 80)                
		std::cout << "you have passed\n";
	else if (marks >= 80)
		std::cout << "excellent job!\n";
	else
		std::cout << "you have failed the exam!\n";

	return 0;
}


Line 10: I'm sure you know this its basically if marks greater than or equal to 60 and marks are less than 80


I know mine can probably be improved greatly, or maybe I'm not doing it the way that you personally are supposed to do it, but the above had worked for me.

Good luck and I let's both learn more!
Last edited on
Ok, but my program is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	const int number = 77;
	int guess;

	cout << "Lets Play a game in which you made a guess of my number\n";
	cout << "Lets Enter your guess:- ";
	cin >> guess;
	cout << "     \n";

	if (guess > number)
		cout << "Too, high guess\n";
	else if (guess < number)
		cout << "Too, low guess\n";
	else (guess == number)
		cout << "correct\n";


	return 0;
}

Error is "Expected ";" before cout".
Why?
Last edited on
is not working

That tells us nothing. What is the problem? A compiler error? A runtime crash? Something else?

We can't read your mind.

EDIT: Line 17 looks wrong to me. An "else" statement doesn't have a condition - it covers all conditions that haven't already been checked for in the "if... else if..." statement.
Last edited on
C/C++ can be a little misleading with error messages sometimes.

the compiler is complaining about a semicolon because it thinks (guess == number) is your statement for the else. It doesn't realise you intended it to be a condition because "else" takes no conditions, it's a "catch everything else" kind of thing.

Error is "Expected ";" before cout".

I see this alot on here, you took the time to copy/paste your code, why not take the time to copy/paste the full error message.
Last edited on
Ok, problem solved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	const int number = 77;
	int guess;

	cout << "Lets Play a game in which you made a guess of my number\n";
	cout << "Lets Enter your guess:- ";
	cin >> guess;
	cout << "     \n";

	if (guess > number)
		cout << "Too, high guess\n";
	else if (guess < number)
		cout << "Too, low guess\n";
	else
	      cout << "correct\n";


	return 0;
}
Last edited on
Pages: 12