Unexpected 'inf' Result

I'm experiencing an unexpected result in my code. This program allows the user to input their points earned on an assignment, and the program is supposed to calculate the user's average, letter grade, and GPA. Unfortunately after the average has been computed the result is always 'inf' instead of the desired value, and the letter grade and average are always 'A' and '4.0'. I believe the problem is in the computeAverage function but I'm not entirely sure. I've played around with the code for a few hours and searched for a solution to this problem but alas, my google-fu has failed me. Does anyone have any ideas on how to resolve this issue? Thanks in advance.

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
107
108
109
110
111
112
113
114
#include <iomanip>
#include <iostream>
#include "CinReader.h"
using namespace std;

class Grader
{
	public:
		
		char computeLetterGrade(double average)
		{
			if (average >= 90)
			{
				return 'A';
			}
			else if (average >= 80)
			{
				return 'B';
			}
			else if (average >= 70)
			{
				return 'C';
			}
			else if (average >= 60)
			{
				return 'D';
			}
			else
			{
				return 'F';
			}
		}
		
		double computeAverage(double pointsEarned, double pointsPossible)
		{
			if (pointsPossible = 0)
			{
				return 0;
			}
			else
			{
				return pointsEarned / pointsPossible;
			}
		}
		
		double computeGPA(char letterGrade)
		{
			switch (letterGrade)
			{
				case 'A':
					return 4.0;
				break;
				case 'B':
					return 3.0;
				break;
				case 'C':
					return 2.0;
				break;
				case 'D':
					return 1.0;
				break;
				default:
					return 0,0;
				break;
			}
		}
};

int main ()
{
	CinReader reader;
	Grader graderOne;
	
	double pointsEarned = 0.0;
	double pointsPossible = 0.0;
	double average = 0.0;
	char letterGrade = 'z';
	double gpa = 0.0;
	
	average = graderOne.computeAverage(0, 0);
	cout << "TEST computeAverage(0,0): " << average << endl;
	
	letterGrade = graderOne.computeLetterGrade(0);
	cout << "TEST computeLetterGrade(0): " << letterGrade << endl;
	
	gpa = graderOne.computeGPA('a');
	cout << "TEST computeGPA('a'): " << gpa << endl;
	
	gpa = graderOne.computeGPA('Z');
	cout << "TEST computeGPA('Z'): " << gpa << endl;
	
	cout << "Enter points earned: ";
	pointsEarned = reader.readDouble();
	
	cout << "Enter points possible: ";
	pointsPossible = reader.readDouble();
	
	average = graderOne.computeAverage(pointsEarned, pointsPossible);
	letterGrade = graderOne.computeLetterGrade(average);
	gpa = graderOne.computeGPA(letterGrade);
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	cout << "Points earned:   " << pointsEarned << endl;
	cout << "Points possible: " << pointsPossible <<endl;
	cout << "Average:         " << average << endl;
	cout << "Letter grade:    " << letterGrade << endl;
	cout << "GPA:             " << gpa << endl;

	return 0;

}
try using == instead of = in your if statement in compute average.
Thank you jonnin, that resolved the the problem. However, now letter grade and GPA always result if 'F' and '0.00' no matter what the average is. Any advice?

edit: nevermind, I was able to resolve the issue on my own. Thank you again for your help, I greatly appreciate it.
Last edited on
Compile with warnings enabled.

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
class Grader
{
    public:

    // ... 

    double computeAverage(double pointsEarned, double pointsPossible)
    {
        // GNU: **warning** suggest parentheses around assignment used as truth value

        // LLVM: **warning** using the result of an assignment as a condition without parentheses 
        //       note:       use '==' to turn this assignment into an equality comparison  

        // Microsoft: **warning** Incorrect operator:  assignment of constant in Boolean context. Consider using '==' instead.
        if (pointsPossible = 0)
        {
            return 0;
        }
        else
        {
            return pointsEarned / pointsPossible;
        }
    }

    // ...

};

http://coliru.stacked-crooked.com/a/768a0417a54b446d
http://rextester.com/GND50565
This is the first I've heard of compiling with warnings enabled. Those error messages are much easier to understand than what I've been getting in powershell.
g++ or clang++: compile with -std=c++14 -Wall -Wextra -pedantic-errors
microsoft: compile with -std:c++14 -W4 -analyze, add -permissive- if the version is current
Topic archived. No new replies allowed.