Average drop lowest C++

Hi guys. I'm currently writing a program that's supposed to find the lowest score drop it and also find the average.
So far I have one more issue that I'm trying to fix. The programs keeps setting the last entry in the loop as the lowest number.

Also a second issue that I haven't been able to fix is getting the program to restart if the wrong number is entered. I have entered an exit function instead into the program


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

void getScore(int&, float&, float&, float&, int&);
void calcAverage (double&,int, float&, float);
int findLowest(int&);


int main ()
{
    float leastscore,Total,score;
    int numTest,min;
    double average;
	cout << fixed << showpoint << setprecision(2);

	getScore(numTest, leastscore, Total, score, min);
	findLowest (min);
	calcAverage(average,numTest,leastscore,Total);

return 0;
}
void getScore(int &numTest, float &leastscore, float &Total, float &score,int &min)
{
	// Gets the number of test scores
	cout << "How many test scores are you entering? ";
	cin >> numTest;
	Total=0;
	leastscore=0;
	

	for (int test = 1; test <=numTest; test++)
	{

		cout << "Enter test " << test<< " score now: ";
		cin >> score;
		leastscore = score;

			if ( score < 1 || score > 100)
			{
				cout << "\nYou have entered an invalid number. \n\n"
                        <<"Re-Enter: ";
			    cin >> score;
			    
			}
            
		        min=0;
				if ( score < leastscore)
                {
                    min = leastscore;
                    
                }


			
	Total+=score;

	}
}

void calcAverage (double& average,int numTest, float &leastscore, float Total)
{
    void getScore(int numTest, float leastscore, float Total, float score);
    void calcAverage (double average);


	average = (Total - leastscore) / ( (numTest) - 1);
	cout << "\nThe average score is: "<<average<<". \n";
}

int findLowest(int& min)
{
void getScore(int numTest, float leastscore, float Total, float score,int min);


cout <<"\nThe lowest score is: "<<min;
return min;
}


I've figured out that each time score is recorded leastscore is set to that number
Last edited on
1
2
3
4
5
6
7
8
9

for (score>0 && score <=100)
{
    leastscore = 100;
    if ( score < leastscore)
    {
        leastscore = score;
    }
}

That isn't what a standard for loop looks like, that shouldn't even compile if you have decent compiler settings.
I think you meant for that to be a if statement?

The programs keeps setting the last entry in the loop as the lowest number.

Look closely. You keep setting leastscore to 100 every iteration. And then compare score to against leastscore... score will always be <= 100. That will always trigger leastscore'a assignment. Set the initial leastscore outside of the loop.

Do you want the whole program to go to the beginning or just have the user re-enter the last number? If the latter, you can do something like this

1
2
3
4
5
6
7
cout << "Enter test " << test<< " score now: ";
cin >> score;
while (score < 1 || score > 100)
{
	cout << "\nYou have entered an invalid number, try again: "
	cin >> score;
}
Yes that is an error that I have since edited


	if ( score < 1 || score > 100)
			{
				cout << "\nYou have entered an invalid number. \n\n"
                        <<"Re-Enter: ";
			    cin >> score;
			    
			}


I've fixed the continue part
Currently my issue is getting it to store the lowest number
Without having to write out comparison statements for each entry of score
I've realized with each run of the program leastscore is getting set to whatever is currently in score

I've edited my OP to the current version i'm working on. Right now I'm still working getting the lowest value. After that I'll edit the calcAverage part to pass the lowest value over.

Ideally I was thinking of something in the realm of score[test] or how to use the test loop. Where everytime a value is entered for score, lastscore compares than value to what it currently has
Last edited on
Next time, don't edit the OP, it makes it easier for others to follow what's going on.

So now you added another variable called min, but that is just complicating it further. What's the difference between leastscore and min?

You are doing the following every iteration:
1
2
3
4
5
6
leastscore = score;
min=0;
if ( score < leastscore)
{
    min = leastscore;
}

score will be equal to leastscore... the logic is flawed.
You should be comparing min against the latest score to see if min is lower than the latest score, and if so, set min to the be the latest score.
Last edited on
OK. I understand. For the OP I accidentally posted the wrong version of the program. As far as score always being equal to leastscore that is my main issue with the program.

I've tried rewriting it using an array but it doesn't run for some reason. Below is the current version i'm working on

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
#include <iostream>
#include<iomanip>

using namespace std;

void getScore(int&, int&, float&, float&);
void calcAverage (double&,int, int&, float);
int findLowest(int&);


int main ()
{
    float Total,score;
    int numTest,leastscore=100;
    double average;
	cout << fixed << showpoint << setprecision(2);

	getScore(numTest, leastscore, Total, score);
	findLowest (leastscore);
	calcAverage(average,numTest,leastscore,Total);

return 0;
}



void getScore(int &numTest, int &leastscore, float &Total, float &score)
{
	// Gets the number of test scores
	cout << "How many test scores are you entering? ";
	cin >> numTest;
	Total=0;
	int test =0;

	while (test <=numTest)
	{

		cout << "Enter test " << test<< " score now: ";
		cin >> score;

			if ( score < 1 || score > 100)
			{
				cout << "\nYou have entered an invalid number. \n\n"
                        <<"Re-Enter: ";
			    cin >> score;
	    
			}
            
		else if(score>0 && score <=100)
			{
			    
				if ( score < leastscore)
                leastscore=score;
                test++;
                
			}
	Total+=score;

	}
}

void calcAverage (double& average,int numTest, int &leastscore, float Total)
{
    void getScore(int numTest, int leastscore, float Total, float score);
    void calcAverage (double average);


	average = (Total - leastscore) / ( (numTest) - 1);
	cout << "\nThe average score is: "<<average<<". \n";
}


int findLowest(int leastscore)

{
    void getScore(int numTest,int leastscore, float Total, float score);

    cout <<"\nThe lowest score is: "<<leastscore;
    return leastscore;
}


it gives me this error
/tmp/ccGUoTSY.o: In function `main':
:(.text.startup+0x59): undefined reference to `findLowest(int&)'
collect2: error: ld returned 1 exit status



My assignment is currently overdue.. so any help will be greatly appreciated

EDIT:
OK. I finally got it to work using the while loop. I can post it if anyone wants to see it
Last edited on
Your function prototype does not match your function definition.
Your function prototype is int findLowest(int&);
but your function definition is int findLowest(int leastscore).
See the difference? int vs int&. int& is a reference to int.

Change the definition to look like int findLowest(int& leastscore) { ... }.
Edit: Good to see you said you got it work
Last edited on
Thanks. I was double checking the synthax of everything and there is was forgot to declare it as a static variable.

The while loop took care of what I was trying to do with DO loop and the array version
Works?

Lets see what you have:
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
#include <iostream>
#include<iomanip>

using namespace std;

void getScore(int&, int&, float&, float&);
void calcAverage (double&,int, int&, float);
int findLowest(int&);

int main ()
{
    float Total, score;
    int numTest, leastscore=100;
    double average;
    cout << fixed << showpoint << setprecision(2);
    getScore(numTest, leastscore, Total, score);
    findLowest (leastscore); // (1) you don't use the returned value. (2) this merely prints a value
    calcAverage(average,numTest,leastscore,Total);
    return 0;
}

void getScore(int &numTest, int &leastscore, float &Total, float &score)
{
	// Gets the number of test scores
	cout << "How many test scores are you entering? ";
	cin >> numTest;
	Total=0;
	int test =0;

	while (test <=numTest) // you will read numTest+1 values
	{
		cout << "Enter test " << test<< " score now: ";
		cin >> score;

		if ( score < 1 || score > 100)
		{
			cout << "\nYou have entered an invalid number. \n\n"
                             <<"Re-Enter: ";

			cin >> score; // you don't test this value
		}
		else if(score>0 && score <=100)
		{
			if ( score < leastscore)
                              leastscore=score;
                       test++;
		}
	        Total+=score;
                // you could have gotten bad value on line 40, but you would
                // add it anyway, without updating the 'test'
	}
}

void calcAverage (double& average,int numTest, int &leastscore, float Total)
{
    // function declaration inside functions are legal, but uncommon
    // you have declared these on lines 6 and 7
    // you don't call getScore or calcAverage in this function
    // void getScore(int numTest, int leastscore, float Total, float score);
    // void calcAverage (double average);

    average = (Total - leastscore) / ( (numTest) - 1); // What if numTest<2 ?
    cout << "\nThe average score is: "<<average<<". \n";
}

int findLowest(int& leastscore)
{
    // function declaration inside functions are legal, but uncommon
    // you have declared getScore on line 6
    // you don't call getScore in this function
    // void getScore(int numTest,int leastscore, float Total, float score);

    cout <<"\nThe lowest score is: "<<leastscore;
    return leastscore; // leastscore was not modified here
}

How many test scores are you entering? 2
Enter test 0 score now: 1000

You have entered an invalid number. 

Re-Enter: 2000
Enter test 0 score now: 5
Enter test 1 score now: 10
Enter test 2 score now: 20

The lowest score is: 5
The average score is: 2030.00. 

Do you think that 2030 is the average of 10 and 20? Should it not be 15?

Wouldn't printNumber be more descriptive name for function that prints a number, than the findLowest?
Last edited on
Topic archived. No new replies allowed.