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;
}