#include <iostream>
usingnamespace 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.
[code]
#include <iostream>
usingnamespace 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;
}
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.
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
ifelseifelseifelse
Doesn't work fine:
1 2 3 4
ifelseifelse // if statement considered over here.
else // so because of that this else is missing an if
#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:-
#include <iostream>
usingnamespace 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.
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.
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:
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.
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.
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";
elseif (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.
#include <iostream>
usingnamespace std;
int main()
{
constint 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";
elseif (guess < number)
cout << "Too, low guess\n";
else (guess == number)
cout << "correct\n";
return 0;
}
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.
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.
#include <iostream>
usingnamespace std;
int main()
{
constint 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";
elseif (guess < number)
cout << "Too, low guess\n";
else
cout << "correct\n";
return 0;
}