Lowest Score Drop (Basic Homework)

Hey guys, I'm sure this exact programming challenge has been posted a multitude of times. I've checked older posts on this forum and although they have helped I'm still running into an issue despite my logic being seemingly all there.

Anyway the homework is to average the HIGHEST 4 out of 5 test scores. I CAN'T use arrays and I MUST use the isValid() function to keep the user input for scores from being less than 0 or greater than 100. I can't put this code inside another function, my professor wants it as a separate function.

Everything executes correctly except after I have put all the scores in my avgScore returns 0.0 every time no matter what I try. The formula for average seems to be corrects but it always shows 0.

Here is my code:

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(double &score);
void calcAvg(double score1, double score2, double score3, double score4, double score5, double lowest);
double findLowest(double score1, double score2, double score3, double score4, double score5);
void isValid();

int main()
{
    double score1, score2, score3, score4, score5, lowest;

    getScore(score1);
    getScore(score2);
    getScore(score3);
    getScore(score4);
    getScore(score5);

    lowest = findLowest(score1, score2, score3, score4, score5);
    calcAvg(score1, score2, score3, score4, score5, lowest);

    return 0;
}

void getScore(double &score)
{
    isValid();
}



void calcAvg(double score1, double score2, double score3, double score4, double score5, double lowest){

    lowest = findLowest(score1, score2, score3, score4, score5);
    double avgScore = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;


    cout << setw(4);
    cout << fixed << showpoint << setprecision(2);
    cout << "The average of the 4 highest scores is " << avgScore << endl;
}

double findLowest(double score1, double score2, double score3, double score4, double score5)
{
    double lowest = score1;

    if (score2 < lowest) {
        lowest = score2;
    } else if (score3 < lowest) {
        lowest = score3;
    } else if (score4 < lowest) {
        lowest = score4;
    } else if (score5 < lowest)
        lowest = score5;

    return lowest;
}

void isValid() {
    double score;

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

    while (score < 1 || score > 100) {
        cout << "Please enter a score between 0-100:";
        cin >> score;
    }

}
Well, your function getScore() doesn't actually get any scores. (Anything entered in isValid() never makes it out of that function, either by return value or reference argument.)

Your logic in findLowest() is also wrong, since the "else if" means you can do at most one comparison. But that's a later problem.

You would do better to keep a running sum and running lowest, rather than five separate numbered scores, if you can't use arrays.

There's no justification for the "float" - wherever you got that from has completely misled you.

Saying "Everything executes correctly except ... " has little to back it up. You should create your code incrementally, testing each new routine as you add it.
Last edited 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
#include <iostream>
#include <iomanip>

double calcAvg(double score1, double score2, double score3, double score4, double score5, double lowest);
double findLowest(double score1, double score2, double score3, double score4, double score5);
double isValid();

int main() {
	const double score1 {isValid()}, score2 {isValid()}, score3 {isValid()}, score4 {isValid()}, score5 {isValid()};
	const double lowest {findLowest(score1, score2, score3, score4, score5)};
	const double avg {calcAvg(score1, score2, score3, score4, score5, lowest)};

	std::cout << std::setw(4) << std::fixed << std::showpoint << std::setprecision(2);
	std::cout << "The average of the 4 highest scores is " << avg << '\n';
}

double calcAvg(double score1, double score2, double score3, double score4, double score5, double lowest) {
	return (score1 + score2 + score3 + score4 + score5 - lowest) / 4.0;
}

double findLowest(double score1, double score2, double score3, double score4, double score5) {
	double lowest {score1};

	if (score2 < lowest)
		lowest = score2;

	if (score3 < lowest)
		lowest = score3;

	if (score4 < lowest)
		lowest = score4;

	if (score5 < lowest)
		lowest = score5;

	return lowest;
}

double isValid() {
	double score {};

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

	while (score < 1 || score > 100) {
		std::cout << "Please enter a score between 0 -100: ";
		std::cin >> score;
	}

	return score;
}

get em while they are getting started, I say :)
-- isvalid should do one thing. it should check if something is valid.
getdata (cin and prompt and complain if isvalid fails) calls isvalid (checks validity, NO couts).
getdata is an 'interface' to the human, and it can do I/O but isvalid should not interact with the human. You want to keep interaction with the human separate from code that does work as a habit.

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
#include <iostream>
using namespace std;

bool isValid( double score )
{
   bool OK = score >= 0 && score <= 100;
   if ( !OK ) cout << "Out of range\n";
   return OK;
}

int main()
{
   const int N = 5;
   double sumall = 0, score, lowest;
   bool first = true;
 
   for ( int cnt = 0; cnt < N; )
   {
      cout << "Enter a score: ";   cin >> score;
      if ( isValid( score ) )
      {
         if ( first || score < lowest ) lowest = score;
         sumall += score;
         cnt++;
         first = false;
      }
   }
   cout << "Average of best " << N - 1 << " = " << ( sumall - lowest ) / ( N - 1 ) << '\n';
}
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
#include <iostream>

bool isValid(double);

int main()
{
   constexpr int num_scores { 4 };
   double sum_scores        { };
   double lowest            { 200.0 };

   for (int cnt { }; cnt < num_scores + 1; ++cnt)
   {
      double score { };

      while (std::cout << "Enter a score: " && std::cin >> score)
      {
         if (!isValid(score))
            std::cout << "** Invalid score! **\n";
         else
            break;
      }
      
      if (score < lowest) lowest = score;
      sum_scores += score;
   }
   std::cout << "Average of best " << num_scores << " = " << (sum_scores - lowest) / num_scores << '\n';
}

bool isValid(double score)
{
   return score >= 0 && score <= 100;;
}
Enter a score: -5
** Invalid score! **
Enter a score: 150
** Invalid score! **
Enter a score: 50
Enter a score: 50
Enter a score: 50
Enter a score: 25
Enter a score: 50
Average of best 4 = 50
Topic archived. No new replies allowed.