Need help correcting loop and function

Hello! I have an assignment where I'm supposed to reuse code from my last assignment (what's inside find_average()) and make the average return as a double. find_average() can read values from the user using cin, but does not print anything.
Then, in the main program I'm to call the function inside a loop that asks the user if they want to repeat the calculation with a new set of scores. The user can enter their answer as y for yes, and n for no.

Basically, my biggest trouble is in main(), because find_average() is perfectly functional. For 7 hours I've been trying to fix everything but I simply can't get it right because it feels like I'm going in circles.

So far, the value of 'average' before the while loop is always -2.38857e+256. If you choose 'n' to run again with new scores at any point in the loop, it says "You have finished your calculations." as it should. If I choose 'y' instead once it reaches the while loop, you will get the correct average after inputting the number of test scores and test scores. So essentially, the first time that this runs:

1
2
3
4
5
6
7
8
9
// Calculates average after asking for number of test scores and then test scores
double average = find_average();

// Gives average, then asks if user wants to rerun the code
cout << "\nYour average is " << average << ".\n\nRun again with new scores? ( y / n )\n";

// User input for YES or NO
cin >> answer;
cout << "\n";


It is the wrong average. How do I fix this?

Any help would be GREATLY appreciated! Here is my entire code thus far:

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

double find_average();

int main()
{
    char answer;
    char y; // y = YES, rerun loop
    char n; // n = NO, stops loop

    // Calculates average after asking for number of test scores and then test scores
    double average = find_average();

    // Gives average, then asks if user wants to rerun the code
    cout << "\nYour average is " << average << ".\n\nRun again with new scores? ( y / n )\n";

    // User input for YES or NO
    cin >> answer;
    cout << "\n";

    // If user inputs y, loop begins
    if(answer == 'y')
    {
        while(answer == 'y')
        {
            // Calculates average after asking for number of test scores and then test scores
            double average = find_average();

            //Gives average, then asks if user wants to rerun the code
            cout << "\nYour average is " << average << ".\n\nRun again with new scores? ( y / n )\n";

            // User input for YES or NO
            cin >> answer;
            cout << "\n";

        }
    }

    // If user inputs n, loop is ignored and calculations end.
    if(answer == 'n')
    {
        cout << "You have finished your calculations.\n";

    }

    return 0;
}

double find_average()
{
    double score, avg, hi;
    double sum = 0;
    double lo = 100;
    int numScores;


    cout << "Please enter how many test scores you want to enter:\n";
    cin >> numScores;

    cout << "\nPlease enter your test scores:\n";

    // Loop if number of scores is more than 0
    for(double counter = 0; counter < numScores; counter++)
    {
        cin >> score;

        // Finding lowest score
        if(score < lo)
            lo = score;
        // Finding highest score
        else if (score > hi)
            hi = score;

        // Finding sum
        sum = sum + score;
    }


    // Finding average without highest and lowest values
    avg = (sum - hi - lo)/(numScores - 2);


    return avg;
}
Last edited on
Sorry very quick response as I am on the way out!!!

If the the math is working correcty inside the loop and incorrectly outside of the loop, I would suggest looking at the code inside the loop that works perfect and remove it to outside of the loop, sorry I cannot provide anymore information at this point.
Last edited on
Line 9-10: What's the point of these variables? They're not used.

Line 23: This if statement is unnecessary. The while statement does the same thing.

Line 52: hi is uninitialized.

Line 64: Don;t use doubles for loop counters. doubles are approximations. You might get a miscompare on the termination condition.

Line 72: You're comparing to an uninitialized variable.

I ran your program after making the above corrections and it appeared to give correct answers.

A general comment on style. It poor practice to have the same logic inside and outside of your loop. Your logic should appear only ONCE. Eliminate lines 12-20. Consider a do/while loop that ensures the loop is executed at least once. Or initialize answer to 'y'.

the value of 'average' before the while loop is always -2.38857e+256.

That result generally indicates you're displaying an uninitialized variable. If you're still getting that error after making the above corrections, please provide the data you're inputting.
Last edited on
Topic archived. No new replies allowed.