I can't see whats wrong with this code

Hi I'm very new to C++ only since yesterday I started learning. I have a problem with the following code wherein if the user inputs an integer outside of the valid if and else if it still says perfect score... were as the if and else if themselves work perfectly. can anyone see what i'm doing wrong?


#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{
int x;

cout<< " Please enter your grade between 0-100:";
cin>> x;
cin.ignore();

if (x <=99)
cout<< "Sorry you didn't get a perfect score!"<<endl;

else if (x=100)
cout<< "Well done perfect score :)"<<endl;

else
cout<<"sorry please restart the proram and enter a valid number";

cin.get();
return 1;
}

I'm using microsoft visual c++ 2010 express edition on a windows 7 machine. Thanks in advanced. I'm working through some very basic beginner activities
Yea, within your if statement you use one = instead of ==. When you use one = , it's assigning the value to x. Use == for if statements

 
if (x = 100) // sets x to 100 


becomes

 
if (x == 100) // checks to see if x is already 100 
Right yeah I see now.. thanks for the reply... maybe I've just been cramming way to much learning in 24 hours. works perfectly
Just a quick suggestion: When main() yields a value other than 0, it usually indicates that something went wrong. Just saying.

Wazzak
Thanks like I said it's been 24 hours including sleep lol so yeah very fresh.... it's alot of fun though. Just like when I used to mess around with Qbasic when I was a kid...
This is an edited version of the original program.


****Right question is how would I get this to loop if the condition of x is not between 0-100..... i've tried the following which doesn't work:

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
32
33
34
35
36
37
38
39
40
//Grading system

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{
int x;
cout<<"Please enter your grade results between 0-100:";
cout<<endl;
cin>>x;
cin.ignore();
do {
if (x < 60)
cout<< "You got an F";
cout<<endl;
else if (x < 70)
cout<< "You got a D";
cout<<endl;
else if (x < 80)
cout<< "You got a C, not bad";
cout<<endl;
else if (x < 90)
cout<< "You got a B, good work";
cout<<endl;
else if (x < 100)
cout<< "You got an A, great work!";
cout<<endl;
else if (x == 100)
cout<< "You got a perfect score A*";
cout<<endl;
else 
cout<< "Sorry please enter a valid number between 0-100";
cout<<endl;
} while ( x ! < 101);
cin.get();
} 


****Also is it possible to make the else if something more along the line of for example:

1
2
3
else if (x <=99) {
cout<< "Well done you got an A!";
}


****meaning less than or equal to or even better how would it be possible to have an else if between two variable integer for example

1
2
3
else if (x <=99||>89) {
cout<< "well done you got an A!";
}



****Meaning less than or equal to 99 but greater than 89.. I know some of this coding is wrong it is shown as an example of what I am trying to achieve...
Do you see where i'm going wrong? apart from having along string of messy else if.

I've even tried the following:

1
2
3
4
5
if (x >= 0 && x <= 59)
			cout<< "You got an F";
		cout<<endl;
		else if (x >= 60 && x <= 69)
			cout<< "You got a D";
etc etc

trying to meet these conditions:

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A

Last edited on
U can use Go to statement and i think u have to remove the Do while loop , another thing u have forgotten that u must put {} In if statements because there are more than 1 .
The Code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Grading system



#include <iostream>

using namespace std;

int main()
{

int x;
cout<<"Please enter your grade results between 0-100:";
cout<<endl;

start:

cin>>x;
cin.ignore();

if (x < 60){
cout<< "You got an F";
cout<<endl;}
else if (x < 70){
cout<< "You got a D";
cout<<endl;}
else if (x < 80){
cout<< "You got a C, not bad";
cout<<endl;}
else if (x < 90){
cout<< "You got a B, good work";
cout<<endl;}
else if (x < 100){
cout<< "You got an A, great work!";
cout<<endl;}
else if (x == 100){
cout<< "You got a perfect score A*";
cout<<endl;}
else {
cout<< "Sorry please enter a valid number between 0-100";
cout<<endl;
goto start;}


cin.get();
} 
do not use goto. Use a do while loop. That's the right way to do it. goto should be avoided when not needed.

1
2
3
4
do
{
  //...
}while(x > 100);
so in theory and practice the following code does work but is considered sloppy and inefficient.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Grading system

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{

	int x;
	cout<<"Please enter your grade results between 0-100:";
	cout<<endl;

start:

	cin>>x;
	cin.ignore();

	if (x <= 59){
	cout<< "You got an F";
	cout<<endl;
	goto end;}
	
	else if (x <= 69){
	cout<< "You got a D";
	cout<<endl;
	goto end;}
	
	else if (x <= 79){
	cout<< "You got a C, not bad";
	cout<<endl;
	goto end;}

	else if (x <= 89){
	cout<< "You got a B, good work";
	cout<<endl;
	goto end;}
	
	else if (x <= 99){
	cout<< "You got an A, great work!";
	cout<<endl;
	goto end;}
	
	else if (x == 100){
	cout<< "You got a perfect score A*";
	cout<<endl;
	goto end;}
	
	else 
	cout<< "Sorry please enter a valid number between 0-100";
	cout<<endl;
	goto start;

end:
	cin.get();

}


______________________________________________


do not use goto. Use a do while loop. That's the right way to do it. goto should be avoided when not needed.

I still can't get the do - while to work so I just wrote it as above... I'll have a play around with it tomorrow


Last edited on
do while loops are very simple:

1
2
3
4
do
{
  // code
}while( /*condition*/ );


The 'code' block is run. Once it reaches the end (the '}' closing brace), the 'condition' is checked. If that condition is true, the 'code' block is run again. And again... until the condition is false.

In your case, you'd want the code to keep running until the user inputs valid code:

1
2
3
4
do
{
  // ... get input from user
}while( /*input is invalid*/ );


invalid input in this case means 'x' is above 100, since anything 100 or below is considered valid.

Hence the outline in my previous post:

1
2
3
4
do
{
  //...
}while(x > 100);
Last edited on
Disch wrote:
goto should be avoided when not needed.
This confuses me...can you name at least one situation where got is needed? I can't think of any :\
well it's never NEEDED, but it can be far more practical than some alternatives.
Ive tried the following code and when you enter an number higher than 100 it says "Sorry please enter a valid number between 0-100" it just loops over and over...


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Grading system

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{

	int x;
	cout<<"Please enter your grade results between 0-100:";
	cout<<endl;

	cin>>x;
	cin.ignore();
	do {
	if (x <= 59){
	cout<< "You got an F";
	cout<<endl;
	}
	
	else if (x <= 69){
	cout<< "You got a D";
	cout<<endl;
	}
	
	else if (x <= 79){
	cout<< "You got a C, not bad";
	cout<<endl;
	}

	else if (x <= 89){
	cout<< "You got a B, good work";
	cout<<endl;
	}
	
	else if (x <= 99){
	cout<< "You got an A, great work!";
	cout<<endl;
	}
	
	else if (x == 100){
	cout<< "You got a perfect score A*";
	cout<<endl;
	}
	
	else if (x > 100){
	cout<< "Sorry please enter a valid number between 0-100";
	cout<<endl;}
	} while (x > 100);

	cin.get();
}
You program logic:
input value to x
do
    code
    if x > 100
        say "please enter it again"
while x > 100

How can they enter it again if you only ever ask them to once before the loop and never again?
woops
works perfectly thank you
I think it's solved thanks guys :)
Topic archived. No new replies allowed.