Letter grades are all E and F

This is a discussion on Letter grades are all E and F within the C++ Programming forums, part of the General Programming Boards category; Hi this program has a serious logical error I can't faddle out. It all works fine when I enter a ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Letter grades are all E and F

    Hi this program has a serious logical error I can't faddle out.
    It all works fine when I enter a score of under 50, but anything
    over this it prints E and ignores the other if statements.
    I even made sure the range is correct before the value is passes to
    the function.

    Code:
    #include <iostream>
    
    // function prototype
    char calculateGrade ( int );
    
    // main function ///////////////////////////////////////////////////////////////
    //
    int main ( void )
    {
       int score;
       
       std::cout << "Enter exam score ( 1 - 100 ): ";
       std::cin >> score;
       
       // validate input is within correct range
       if (( score < 1 ) || ( score > 100 ))
       {
          std::cout << "\nERROR: Invalid input, terminating..." << std::endl;
       }
       
       else
       {
          std::cout << "\nGrade attained: " << calculateGrade ( score ) << "\n";
       }
       
       std::cin.get(); // freeze console window output
    	std::cin.ignore(); 	
    	 
       return 0; // indicate program terminated sucessfully
    }
    
    // function to calculate and return letter grade dependant
    // on value of score number passed
    char calculateGrade ( int x )
    {
       char grade = '/0'; // nulled for some kind of assignment
       
       if (( x == 100 ) || ( x >= 90 ))
          grade = 'A';
          
       if (( x == 89 ) || ( x >= 80 ))
          grade = 'B';
          
       if (( x == 79 ) || ( x >= 70 ))
          grade = 'C';
          
       if (( x == 69 ) || ( x >= 60 ))
          grade = 'D';
          
       if (( x == 59 ) || ( x >= 50 ))
          grade = 'E';
          
       if ( x < 50 )
          grade = 'F';
          
       return grade;
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You really should use AND instead of OR. Ex.
    Code:
    if(x <=100 && x >= 90)
    grade='A';
    I think the use of OR might be causing conflicts with other if statements.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    First, you should be using else/ifs, so you don't have to execute every test each time. Second, each test should be using "&&", not "||". Third, you can simplify the logic, with something like:
    Code:
      if (x < 50) grade = 'F';
      else if (x <= 59) grade = 'E';
      else if (x <= 69) grade = 'D';
      // etc.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    That worked thanks Sentral. The reason I used OR is because I need either expression to be TRUE for the assignmnet. Although seeing it, AND works the same as the >= operator ensures both conditions are TRUE. Doh! Silly mistake, although an easy one to make. Thanks again for the help.

    EDIT ok, il change my logic, I just wanted to get it working ok before I made any changes to decrease the program size and maximise the logic
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21