Broken program

I need to create a function that can find the average score. I came up with this
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>
#include <iomanip>
using namespace std;
// This program displays the average of quiz scores. You may assume that each quiz score will be entered
// as an is an integer in the range [0, 10].
int main()
{
    int count = 0, score, sum;
    cout << "Enter quiz score (negative value to quit)? ";
    cin >> score;
    while (score > 0) 
    {
        sum += score;
        ++count;
        cout << "Enter quiz score (negative value to quit)? ";
        cin >> score;
    }
    if (count = 0) 
    {
        cout << "No average could be calculated." << endl;
    } 
    else 
    {
        double avg = static_cast<double>(sum) / count;
        cout << fixed << setprecision(1);
        cout << "The average is " << avg << endl;
    }
    return 0;
}

And it's not working, can you help me fix some bugs? I need it fixed by tomorrow.
Line 18 is wrong. It should say if (count == 0) This is a common typo that your compiler should warn you about.
Also, on line 8, you should initialize sum to be zero. Better yet, put all those declarations on separate lines so it is clear.
int count = 0, score, sum; becomes
1
2
3
int count = 0;
int score = 0;
int sum = 0;
Last edited on
Thank You!
Topic archived. No new replies allowed.