The other day i saw someone was attempting this task, and i thought it would be within my newbie abilities to give it a try.
The idea was to instruct the user to enter values and then -1 when finished, this would break the loop and calculate an average of the given values:
CODE
#include <iostream>
using namespace std;
int main()
{
float count = 0;
float temp = 0;
float total = 0;
float average = 0;
cout << "Please enter a series of positive numbers \n";
cout << "Enter -1 when finished \n";
cout << "Press enter to continue \n";
cin.get();
while (temp != -1)
{
cout << "Number " << count + 1 << " : ";
cin >> temp;
cin.ignore();
if (temp != -1);
{
total = total + temp;
count = count + 1;
}
}
cout << "You entered " << count + 1 << " values\n";
cout << "The total was " << total << "\n";
average = total / count;
cout << "The average is " << average << "\n";
cin.get();
}
So, im sure to be poked by dreamincode's advanced c++ programmers as to technique and where i may be doing something a little incorrectly. This is very welcome!
However, the problem im having is that Count at the end is 2 higher than it should be and Total is adding the negative 1.
Im sure im making a really stupid mistake somewhere, but running that code through my head it seems to work fine. Any ideas?
Thanks