logic error i believe

My task is to write a simple program that takes in an unknown amount of numbers until the sentinel value is given then proceed to give out the average. The code compiles but i doesn't give back the right results. Can someone point out what was my mistake? This is not my homework assignment i do this as a hobby.

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

using namespace std;

int main()
{
        int counter = 0;
        int sentinal = 9999;
        float theInput;
        float sum=0;
        float average;
        cout << 25/5 << endl <<endl;
        while (theInput!=9999)
{
        cout << "Please input the numbers to be averaged (9999 to finish up) " << endl;
        cin >> theInput;
        
        sum=theInput+sum;
        counter++;
}
        average=sum/counter;

        cout << "The average for the " << counter << " numbers is " << fixed  << average << endl;

        return 0;
}

at the beginning i used one of the iomanip function but took it out
that's because it considers 9999 a valid number.
if you'd only enter 9999, you'd get 9999.
a solution would be to check if theInput == sentinal and break if so on line 18. if you do that, you won't need to check on line 14.
Topic archived. No new replies allowed.