Calculator Help

Ok so im writing a calculator and im working on a function that calculates the mean, and it seems to be off and im not sure why, i entered

23
45
32
78
Answer: 28.75

and when i check it on a different calculator, the mean is 44.5 so what is wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Calculator::FindMean()
{
    vector<float> V_storeNumbers;
    float inputNumber;
    int number = 1;

    cout << "Enter Numbers, enter stop to stop" << endl;

    while(inputNumber != -1)
    {
        cout << "Number " << number++ << ": ";
        cin >> inputNumber;
        V_storeNumbers.push_back(inputNumber);
    }

    V_storeNumbers.pop_back();

    for(int j = 0; j < 1; j++)
    {
        cout << V_storeNumbers[j] + V_storeNumbers[j] / V_storeNumbers.size() << endl;
    }
}
Hi Ch1156, could you give us the whole code of interest, together with the output ?
Also, I would rather not use you stop method, it's a bit unsafe when asking for an int, to write a string.

Finally I don't quite understand the purpose of the last for loop, could you explain what you do there ?
what you are basically doing is cout << 23 + 23 / 4 << endl; order of operations it is 23 + 5.75

What you want to do is sum all the values in the vector and then divide by the size.
Last edited on
"What you want to do is sum all the values in the vector"

How do i do that? I am really unsure how i add all the values from the vector. @aleonard the last for loop outputs all the elements, it was there during debugging when i was making sure all numbers where being assigned to an element, it will be removed when this function is complete.
Nevermind i figured it out
Topic archived. No new replies allowed.