help with if-else statements

Hey guys total newbie here this grading software excercise is giving me trouble.
I cant get the second else if to give me the output B. Here are the instructions.

//Grading Program: 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



float grade;


int main() {
cout << "This is a grading software" << endl;
cout << "Please Enter Your Grade" << endl;

cin >> grade;




if (grade == 100) {
cout << "you got perfect!";
}
else if (grade > 89 && grade < 100) {
cout << "A";
}
else if (grade > 79 && grade < 90) {
cout << "B";
}

return 0;

}
[/code]
Missed the first code tag lol

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
float grade;


int main() {
cout << "This is a grading software" << endl;
cout << "Please Enter Your Grade" << endl;

cin >> grade;




if (grade == 100) {
cout << "you got perfect!";
}
else if (grade > 89 && grade < 100) {
cout << "A";
}
else if (grade > 79 && grade < 90) {
cout << "B";
}

return 0;

}


I also ran the program with no errors:
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>
#include <string>


float grade;



int main() {
std::cout << "This is a grading software" << std::endl;
std::cout << "Please Enter Your Grade" << std::endl;

std::cin >> grade;




if (grade == 100) {
std::cout << "you got perfect!";
}
else if (grade > 89 && grade < 100) {
std::cout << "A";
}
else if (grade > 79 && grade < 90) {
std::cout << "B";
}

return 0;

}
Last edited on
Hello codefox10,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Tested your code and it worked fine. I did make two changed: 1. changed "float grade" to "double grade" because floats do not always store the decimal portion correctly. 2. Moved "double grade" inside main because it does not need to be a global variable and global variables are should not be used for most programs.

If you are still having problems maybe some more detail about what is going wrong.

Hope that helps,

Andy
So you guys ran the program without any problems? When I put an input in like 85 I don't get an output it just says "press any key to continue..."

Thanks everyone
Topic archived. No new replies allowed.