'double' division problem?

I have this program that is similar to my last, but uses functions instead.

I am having the problem of displaying 0.00 for all my outcomes. Why is this? I keep getting hung up on simple problems like this.


EDIT EDIT: Ok, I changed the datatype of the variables being passed to calcScore to double's and now it displays a number... but the number isn't exactly right. Why does it give me like 99.62 when I pass it perfect scores?




Here's my code:

Line 71-90 is where I bet the problem is... calculating those score outputs.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;


double programavg = 0;
double programscores = 0;
double programmax;

double quizavg = 0;
double quizscores = 0;
double quizmax;

double testavg = 0;
double testscores = 0;
double testmax;

char choicemore = 'y';
double examavg;
double overallavg;

/* This is the getIntSum function */ 


int getIntSum(string s, int exitVal)
{
int addpoints;
int totalpoints = 0;;

	while (addpoints!=exitVal)
	{
		cout << s << exitVal << " to quit):";
		cin >> addpoints;
		
		
		totalpoints+=addpoints;
	}
	
		cout << endl;
		return totalpoints;
	
	
}


/* This is the getPromptedInt function */

int getPromptedInt(string s, int min, int max)
{
int maxpoints;

cout << s << min << "..." << max << "):"; 
cin >> maxpoints;
cout << endl;

	while (maxpoints>max || maxpoints<min)
	{
		cout << "Error: value must be between " 
		<< min << " and " << max << ". Please re-enter:";
		cin >> maxpoints;
		cout << endl;
	}

		return maxpoints;
	
	
}

/*This is the calcScore function */

double calcScore(double sum, double maxPoss)
{

double finalscore;

	if (maxPoss>0)
	{
		finalscore = (sum / maxPoss);
		finalscore*=100;

		return finalscore;
	}
	else
	{
		return 0;
	}

}


/* This is the main function --------------------*/

int main()
{
/* This loop is for the purpose of repeating while the user wants to */


while (choicemore=='y')
{
programmax = getPromptedInt("Enter the maximum possible program points (", 0, 1200);
programscores = getIntSum("Enter a program score (", -1);

quizmax = getPromptedInt("Enter the maximum possible quiz points (", 0, 120);
quizscores = getIntSum("Enter a quiz score (", -1);

testmax = getPromptedInt("Enter the maximum possible test points (", 0, 300);
testscores = getIntSum("Enter a test score (", -1);


programavg = calcScore(programscores, programmax);
quizavg = calcScore(quizscores, quizmax);
testavg = calcScore(testscores, testmax); 


cout << fixed << setprecision(2) << "Program average is: " << programavg;
cout << fixed << setprecision(2) << endl << "Quiz average is " << quizavg;
cout << fixed << setprecision(2) << endl << "Test average is: " << testavg;

//  The following loop is to calculate the exam average
	if (testavg==0)
	{
		examavg = quizavg;
		cout << setprecision(2) << fixed << endl << "Exam average is: " << examavg;
	}
	else
	{
		examavg = ((((testavg * 3) + quizavg)) / 4);
		cout << setprecision(2) << fixed << endl << "Exam average is: " << examavg;
	}	
	
// This loop is for displaying the Overall average

	if (testavg==0)
	{	 
		overallavg = ((.6*quizavg) + (.4*programavg));
		cout << endl << "Overall average is: " << overallavg;
	}
	else
	{
		overallavg = ((.4*programavg) + (.15*quizavg) + (.45*testavg));
		cout << endl << "Overall average is: " << overallavg;
	}
	

// This next loop is the warning message for low test scores
	if (examavg<55)
	{
		cout << endl << endl << "*** Your exam average is below 55%! ***" << endl;
	}
	else
	{
	cout << endl;
	}	   
	
cout << endl << "Another (y/n)? ";
cin >> choicemore;
cout << endl;

}

return 0;

}
Last edited on
Someone PLEASE help me? I can't imagine it's some major issue.
I am surprised that your program does compile.
In line 80 you write
finalscore = (sum / maxPoss);

But I do not see where you define and initilize your variable sum.
And if something is 0, the result of its division is 0 as wel.

int main
But I do not see where you define and initilize your variable sum.
double calcScore(double sum, double maxPoss)

I was playing around with your code and can't seem to figure out the problem either, so i am interested to see where the problem is too.
Well, it is most likely the fact that double division sucks, if you do seemingly simple stuff (divide by ints, etc) it usually has lots of problems...like the 99.67 instead of 100. It is most likely because of the avgs*.4/.6 that is screwing it up. You could try couting the avgs as they are made for a debug, and see if they are glitched in some way. If they are, it is probably because of double errors.
Someone PLEASE help me? I can't imagine it's some major issue.

Wow. You waited like... an WHOLE HOUR before bumping your post. How rude of all the people here not to be waiting for the possibility to answer your questions ASAP.

Why does it give me like 99.62 when I pass it perfect scores?

What should the output be? What influences it? In line 73, what values do 'sum' and 'maxPoss' have?

Well, it is most likely the fact that double division sucks

Yeah, right. I don't understand it, I've heared that it might be wrong sometimes, so it sucks - how obvious. As I argued in my article about double arithmetic (http://www.cplusplus.com/forum/articles/3827/), there *are* problems with it. But to get a noticable error when not performing (in)equlity tests that has any significance, you have to perform quite some calculations or have very "ugly" numbers. So if the result should be 100, getting 99.62 as the result would require the formula x = (a/b)*100 to have (a/b) in the order of 245 or somewhere near that. Is that the case? I doubt it.
Topic archived. No new replies allowed.