How to stop...

i am currently working on the "Grading Program" that requires an "if statement to check the input that the uses enters on a scale of0 - 100 to be able to out put a grade! (i am following thde learning pattern given in this link... http://www.cplusplus.com/forum/articles/12974/ )

Now, on to my question...

i would really like some assistance in figuring out how to end this "if - else if" statement... there are currently 2 of them here at the moment, but a way to end one would solve both of them :D !

this is what is troubling me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if score < 0 or score > 100
		cout<<"The grade you have selected doesn't match this grading scale1"<<endl;
	//above code is used to make sure that the 
	//scale of 1 - 100 is used!

	if score >= 0 and score <= 59
		cout<<"You recieve an F for your grade"<<endl;
	else if score >= 60 and score <= 69
		cout<<"You recieve a D for your grade"<<endl;
	else if score >= 70 and score <= 79
		cout<<"You recieve a C for your grade"<<endl;
	else if score >= 80 and score <= 89
		cout<<"You recieve a B for your grade"<<endl;
	else if score >= 90 and score <= 100
		cout<<"You recieve an A for your grade"<<endl;
	
	//above code is used to be able to set a boundry for
	//the grading scale, so that diffrent grades can be
	//used for the score achieved! 


also, help in understanding "switch statements would be a great help ! any help would be appriciated! ty in advance!
Last edited on
What exactly are you having trouble with? Btw, you should surround your ifs/else ifs with ().

You could probably find all the information you need here:
http://www.cplusplus.com/doc/tutorial/control/
This block of code is still giveing me problems...

in the Build Section, the errors that it is outputting to me are as follows...
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(11): error C2061: syntax error : identifier 'score'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(14): error C2143: syntax error : missing ';' before ')'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(22): error C2181: illegal else without matching if
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(22): error C2061: syntax error : identifier 'score'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(24): error C2143: syntax error : missing ')' before ';'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(25): error C2059: syntax error : ')'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(26): error C2181: illegal else without matching if
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(26): error C2061: syntax error : identifier 'score'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(28): error C2143: syntax error : missing ')' before ';'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(29): error C2059: syntax error : ')'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(30): error C2181: illegal else without matching if
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(30): error C2061: syntax error : identifier 'score'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(32): error C2143: syntax error : missing ')' before ';'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(33): error C2059: syntax error : ')'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(34): error C2181: illegal else without matching if
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(34): error C2061: syntax error : identifier 'score'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(36): error C2143: syntax error : missing ')' before ';'
1>c:\users\trey\documents\visual studio 2010\projects\grading program\grading program\gp source code.cpp(37): error C2059: syntax error : ')'




I really want to understand this but thereis too much crap and i NEED someone to make it simple :P i know what your probably going to say is that there is no SIMPLE explination, but i need someone to make it a slight bit easier :P

any help would be greatly appriciated! ty
Also and should be &&
and or should be ||
Did you wrap the condition in the if statement in parentheses?

Like binarybob350 said, you cannot use "and" or "or". You must use "&&" or "||".

Like this:
if (score >= 0 && score <= 59)

Also, the first line of code is wrong.

if (score < 0 || score > 100)
(I went ahead and correct the syntax)

This will make it where you can't enter any number between 1 and 100. You must use "&&" and invert your <> symbols.

Last edited on
Switch statement: http://www.youtube.com/watch?v=181v7ZQ2uVA

Also when you use an if, else if, or else statement you should use braces and parentheses:

1
2
3
4
5
6
7
8
9
10
11
12
if (someThing == someThingElse)
{
// Do something
}
else if (someThing == someThingElse)
{
// Do something
}
else
{
// Do something
}


Also, this isn't needed but it can shorten code. Make your result console output commands more like this:

1
2
3
4
cout << "You recieve an ";
if (score <= 59) {cout << "F ";}
else if (score >= 60) {cout << "D ";}
cout << "for your grade";


I only used two possible grades but you should get the idea.


I hope this post helps you.
Topic archived. No new replies allowed.