problem with a nested if using some simple logical operators

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:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace 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";}                                                    
    else if (cAverage>=80&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "A-";} 
    else if (cAverage>=80) {lGrade = "B";}
    else if (cAverage>=70&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "B-";}                                              
    else if (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 
Try this

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//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>
using namespace 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";
	else if(average>=80)
	{
		if(check==true)
			grade="A-";
		else
			grade="B";
	}
	else if(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.
Last edited on
In your program everything works perfect, only thing u need add brackets in the if condition

i have made bold and underlined below
else if (cAverage>=80&&(FstTscore>=95||SndTscore>=95||TrdTscore>=95))

([/b]cAverage>=70&&(FstTscore>=95||SndTscore>=95||TrdTscore>=95))
Topic archived. No new replies allowed.