I am getting a very minor error in my program, hopefully someone will be able to fix this easily (I am definitely a beginner): I need my program to output grades for an average of four program scores, three test scores, a course average, and a letter grade for the course. Here is the grading scale for the letter grade:
>=90 :A
80-89:B (HOWEVER, if any of the test scores is >=95, the letter grade is a A-)
70-79:C (HOWEVER, if any of the test scores is >=95, the letter grade is a B-)
<70:F
My program works fine, all the letter grades output correctly except for ONE: when it is supposed to output a B-, it INSTEAD OUTPUTS AN A-. Here is the code, I assume that is an the issue with the nested if:
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
int main (){
system ("color f0");
//Do while loop for another student
char anotherStudent;
do
{
//declarations
string fName,lName,lGrade;
char mInitial;
float FstPscore,SndPscore,TrdPscore,FrthPscore;
float FstTscore,SndTscore,TrdTscore;
float pAverage,tAverage,cAverage;
//Program Input
cout<<"Student Grade Calculator"<<endl;
cout<<endl;
cout<<"Please enter your first name, middle initial, and last name: "<<endl;
cin>>fName>>mInitial>>lName;
cout<<endl;
cout<<"Thank you, your name is "<<fName<<" "<<mInitial<<" "<<lName<<endl;
cout<<endl;
cout<<"Please enter the first program score <0-100>: ";
cin>>FstPscore;
cout<<endl;
cout<<"You entered "<<FstPscore<<endl;
cout<<endl;
cout<<"Please enter the second program score <0-100>: ";
cin>>SndPscore;
cout<<endl;
cout<<"You entered "<<SndPscore<<endl;
cout<<endl;
cout<<"Please enter the third program score <0-100>: ";
cin>>TrdPscore;
cout<<endl;
cout<<"You entered "<<TrdPscore<<endl;
cout<<endl;
cout<<"Please enter the fourth program score <0-100>: ";
cin>>FrthPscore;
cout<<endl;
cout<<"You entered "<<FrthPscore<<endl;
cout<<endl;
cout<<"Please enter the first test score <0-100>: ";
cin>>FstTscore;
cout<<endl;
cout<<"You entered "<<FstTscore<<endl;
cout<<endl;
cout<<"Please enter the second test score <0-100>: ";
cin>>SndTscore;
cout<<endl;
cout<<"You entered "<<SndTscore<<endl;
cout<<endl;
cout<<"Please enter the third test score <0-100>: ";
cin>>TrdTscore;
cout<<endl;
cout<<"You entered "<<TrdTscore<<endl;
cout<<endl;
//Average Calculations
pAverage=(FstPscore+SndPscore+TrdPscore+FrthPscore)/4;
tAverage=(FstTscore+SndTscore+TrdTscore)/3;
cAverage=(pAverage+tAverage)/2;
//Nested If
if (cAverage>=90) {lGrade = "A";}
elseif (cAverage>=80&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "A-";}
elseif (cAverage>=80) {lGrade = "B";}
elseif (cAverage>=70&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "B-";}
elseif (cAverage>=70) {lGrade = "C";}
else {lGrade = "F";}
//Program Output
cout<<"123456789012345678901234567890123456789012345"<<endl;//45 spaces
cout<<setfill('*');
cout<<setw(45)<<"*"<<endl;
cout<<"Name: "<<fName<<" "<<mInitial<<" "<<lName<<endl;
cout<<endl;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Program Average.......... "<<pAverage<<endl;
cout<<endl;
cout<<"Test Average............. "<<tAverage<<endl;
cout<<endl;
cout<<"Course Average........... "<<cAverage<<endl;
cout<<endl;
cout<<"Letter Grade............. "<<lGrade<<endl;
cout<<endl;
cout<<setfill('*');
cout<<setw(45)<<"*"<<endl;
//2nd part of do while loop
cout<<"Please enter Y for another student or N to quit:";
cin>>anotherStudent;
cout<<endl;
}while(anotherStudent=='y');
cout<<endl;
system ("pause");
return 0;
}//end of main
//I need my program to output grades for an average of four program scores, three test scores, a course average, and a letter grade for the course.
//Here is the grading scale for the letter grade:
//>=90 :A
//80-89:B (HOWEVER, if any of the test scores is >=95, the letter grade is a A-)
//70-79:C (HOWEVER, if any of the test scores is >=95, the letter grade is a B-)
//<70:F
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string grade;
bool check=false;
double program[4];
double total_program_score=0;
double test[3];
double total_test_score=0;
double average;
for(int i=0;i<4;i++)
{
cout<<"enter "<<i+1<<" program score: ";
cin>>program[i];
cout<<endl;
total_program_score+=program[i];
}
for(int i=0;i<3;i++)
{
cout<<"Enter "<<i+1<<" score: ";
cin>>test[i];
if(test[i]>=95)
check=true;
cout<<endl;
total_test_score+=test[i];
}
cout<<check;
total_program_score/=4;
total_test_score/=3;
average=(total_program_score+total_test_score)/2;
//>=90 :A
//80-89:B (HOWEVER, if any of the test scores is >=95, the letter grade is a A-)
//70-79:C (HOWEVER, if any of the test scores is >=95, the letter grade is a B-)
//<70:F
if(average>=90)
grade="A";
elseif(average>=80)
{
if(check==true)
grade="A-";
else
grade="B";
}
elseif(average>=70)
{
if(check==true)
grade="B-";
else
grade="C";
}
else
grade="F";
cout<<"The grade for the given test score: "<<grade<<endl;
system("pause");
return 0;
}
The problem could be to do with the binary fraction representation of floating point - not all numbers can be represented exactly so direct comparisons very often fail.
You need to calc the absolute value of (your number minus the comparison number), then compare this with an appropriate scaled epsilon value.
Google numeric_limits<double>epsilon
Changing the type to double doesn't fix the problem, it just means it will happen less often. I routinely use doubles instead of floats. I also always code double values as 70.0 as opposed to 70 so there is no chance the compiler might think it is an int.