Something's wrong with this program (else if)

So I felt like trying to do some practice problem programs to practice what I've learned so far in C++ (which is very little).

I tried to do one that you enter a number and the program tells you what letter grade it is.

For some reason mine isn't working correctly though - it seems to say every score is an F even when I enter something that shouldn't do that.

This is 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
47
48
49
50
51
52
53
54
55
56
57
//December 2011
//Gives letter grade based on score inputted

#include <iostream> 
#include <limits> 
using namespace std; 

int pause () 
{
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
} 

int main () 
{
    int score; 
    cout << "This program tells you a letter grade based on your input."; 
    cout << "\nEnter a score you got (out of 100) to be turned to letter grade.";
    while (score >= 0) {
          cout << "\nEnter the score: "; 
          cin >> score;
          if (0 <= score <= 59) {
                cout << "Aww, that's an F."; }
          else if (60 <= score <= 62) {
               cout << "You got a D-.";  }
          else if (63 <= score <= 66) {
               cout << "You got a D."; }
          else if (67 <= score <= 69) {
               cout << "You got a D+."; } 
          else if (70 <= score <= 72) {
               cout << "That's a C-."; }
          else if (73 <= score <= 76) {
               cout << "That's a C."; }
          else if (77 <= score <= 79) {
               cout << "C+. It's like C++, but its not."; } 
          else if (80 <= score <= 82) {
               cout << "You got a B-."; } 
          else if (83 <= score <= 86) {
               cout << "You got a B."; } 
          else if (87 <= score <= 89) {
               cout << "You got a B+."; } 
          else if (90 <= score <= 92) {
               cout << "Awesome, that's an A-!"; } 
          else if (93 <= score <= 96) {
               cout << "Awesome, you got an A!"; } 
          else if (97 <= score <= 99) { 
               cout << "Awesome, that's an A+!"; } 
          else if (score == 100) {
               cout << "You got a perfect 100%!"; } 
          else if (score > 100) {
               cout << "Even more than A+!"; }
          else { 
               cout << "Error - invalid input!"; } 
          cout << "\nPress the enter key to do this again. "; 
          pause ();    }   
    return 0; 
}       


When I run, this is what happens:

This program tells you a letter grade based on your input. 
Enter a score you got (out of 100) to be turned into a letter grade.
Enter the score: 80 
That's an F!
Press the enter key to do this again.

Enter the score: 78
That's an F! 
Press the enter key to do this again. 



What's the error in the code?

Thanks,

Apple Cider
The very first condition (and all consequent ones) is wrong:
if (0 <= score <= 59)
I suppose that the compiler interprets this as
if((0 <= score) <= 59)
The expression into the internal parenthesis is true, then it promotes to 1. And the whole condition always holds.

Modify it to something like it
if(code >= 0 && code <= 59)
I'll try that and see if it works
it works perfectly now
thanks!
Topic archived. No new replies allowed.