How to make my code return decimal precision?

I don't know what I'm doing wrong. My code seems to work fine otherwise, but I can't get the average to print the proper answer with decimal.

It's an assignment for my beginning C++ course, where I'm supposed to have functions which can intake 5 test scores, drop the lowest and then give the average of the 4 highest scores. Everything seems to be working correctly except that the averages are whole numbers without the mathematically correct decimals. i.e. 45.50 reads as only 45.00. I have already changed all of my variables from int to double, and included iomanip precision but that didn't work. please help

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

#include <iostream>
#include <iomanip>
using namespace std;

int getScore(double userInput);
int calcAverage(double testScore1, double testScore2, double testScore3, double testScore4, double testScore5, double lowest, double avgMinusLowest);
int findLowest(double testScore1, double testScore2, double testScore3, double testScore4, double testScore5);

int main()
{
	double testScore1 = 0, testScore2 = 0, testScore3 = 0, testScore4 = 0, testScore5 = 0, lowest = 0, average, avgMinusLowest = 0;

	testScore1 = getScore(testScore1);
	testScore2 = getScore(testScore2);
	testScore3 = getScore(testScore3);
	testScore4 = getScore(testScore4);
	testScore5 = getScore(testScore5);

	average = calcAverage(testScore1, testScore2, testScore3, testScore4, testScore5, lowest, avgMinusLowest);
	cout << "The average is " << average << endl;
	
	return 0;
}

int getScore(double userInput)
{
	cout << "Enter a test score: ";
	cin >> userInput;

	while (userInput < 1 || userInput > 100)
	{
		cout << "Error! You must enter a value from 0-100. Try again: ";
		cin >> userInput;

	}

	return userInput;
}

int calcAverage(double testScore1, double testScore2, double testScore3, double testScore4, double testScore5, double lowest, double avgMinusLowest)
{

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	avgMinusLowest = ((testScore1 + testScore2 + testScore3 + testScore4 + testScore5 - lowest) / 4);

	cout << fixed << showpoint << setprecision(2);
	return avgMinusLowest;
}

int findLowest(double testScore1, double testScore2, double testScore3, double testScore4, double testScore5)
{
	double lowest;
	lowest = testScore1;

	if (testScore2 < lowest)
		lowest = testScore2;
	else if (testScore3 < lowest)
		lowest = testScore3;
	else if (testScore4 < lowest)
		lowest = testScore4;
	else if (testScore5 < lowest)
		lowest = testScore5;

	cout << "The lowest score is: " << lowest << endl;

	return lowest;
}
Check the return type of your functions.

[edit: And pay attention to your compiler warnings, since they were likely warning you of the 'double' to 'int' conversion going on.]
Last edited on
Duh!!! Thank you! Boy that was driving me crazy.

I wasn't getting any compiler warnings, the prog was running fine, but your comment helped me fix it. Thank you so much
Last edited on
Topic archived. No new replies allowed.