Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am having trouble with my code. This is my first C++ project. I keep getting the error on my identifier, but it works fine earlier in the code. Any tips? Thank yo

C++
#include <iostream>
using namespace std;


int main()
{
		int finalScore;
		cout << "Please enter your final test score  > ";
		cin >> finalScore;

		{if (finalScore >= 60)
			cout << "Congratulations you have passed";

		else if (finalScore < 60)
			cout << "You have failed.Next time try harder."; }

		system("PAUSE");
	return 0;

}
Posted
Updated 20-Oct-14 7:56am
v2
Comments
CPallini 20-Oct-14 13:52pm    
The posted code compiles fine on my system.
cstudent1 20-Oct-14 14:02pm    
I am using visual basic, what are you using?
CPallini 20-Oct-14 16:25pm    
Are you kidding?
cstudent1 20-Oct-14 14:05pm    
I tried using dev c++ and it compiles, but it shows both statements.
CPallini 20-Oct-14 16:26pm    
What?

If you write
C++
#include <iostream>

there won't be any compilation errors. If you have some errors, they cannot be related with this identifier. Check up what's in the #include line. I have only on explanation: your observation is incorrect. Rebuild the project cleanly.

Solution 1 is wrong, but the advice is correct: you don't need these extra brackets.
Also, replace else if (finalScore < 60) with else. Do I even have to explain why? Also, avoid immediate constants like 60, instead, declare explicit constant, otherwise your code would be bad for maintenance.

—SA
 
Share this answer
 
v3
Comments
cstudent1 20-Oct-14 14:29pm    
My book, as well as several sample codes show else if, why is it better just to say else. I googled immediate constant and nothing came up. What does this mean? The program should return one value if the grade is higher than 60 and another if it is lower. How else would I do this?
Sergey Alexandrovich Kryukov 20-Oct-14 15:42pm    
Just think a bit. Even though your code is equivalent, its quality is just unacceptable. Anything redundant is unacceptable. I leave for your home exercise why is that "else" condition redundant. I answered "how else". Replace "else if (finalScore < 60)" with "else".

I verified your code. It compiles well under the condition I mentioned. Will you accept the answer formally now (green "Accept" button)?

—SA
CPallini 20-Oct-14 16:01pm    
Hence return 0; is inaccettable too. :-)
Sergey Alexandrovich Kryukov 20-Oct-14 16:05pm    
Why? Do you mean it should be void, when return is not essentially used, or what? In all cases, this is irrelevant.
Redundant "if else" is clearly unacceptable, because the condition checks up the complement subset, which is always true, by definition.
—SA
CPallini 20-Oct-14 16:23pm    
In C++
return 0;
is redundant as it is implicitely assumed as main exits. See
http://en.cppreference.com/w/cpp/language/main_function
(point 4)
I tried removing the curly braces, but it does not change anything. It seems like it is not reading the variable finalScore, but I am not sure why. I have followed the format example from my book, and have tried googling this, but am still lost. Any help would be appreciated.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Oct-14 14:07pm    
Of course it would not change anything! That solution is simply wrong; I explained it in my comments and my answer.
Please don't post such content as "solution" — it is not. Instead, use comments.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900