Hi, I was trying to make a program that calculates the arithmetic average of all the inputed numbers. The program works but there's one problem. I don't know how to tell the program to output the result because there are no more inputed numbers. Any ideas ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main () {
cout.setf(ios::fixed);
cout.precision(2);
double n;
double suma = 0;
int d = 0 ;
double mitjana;
while (cin>>n) {
suma= suma + n;
++d;
}
mitjana = suma/d;
cout << mitjana << endl;
}
Choose one:
1) Manually send end of stream by pressing Ctrl+D (Linux) or Ctrl+Z (Windows)
2) Enter a non-number
3) select a number to act as sntiel and stop input when it is entered
Thanks for your reply!
I already noticed that when I entered a non number I obtained the output. But what I was trying to say ( sorry I didn't express myself clearly in the last post) is if there's any way that if I input in a single line a bunch of numbers separated by a space, example: 23 65 78 23 , when I press return the program gives me the output.