I'd like to make a program that keeps reading numbers until there is an empty input. What I mean by that is the following:
12 <ENTER>
24 <ENTER>
<ENTER>
Sum of these numbers is: 36
So far I've got this:
#include<iostream>
using namespace std;
long double sum = 0, num = 0;
string junk;
int main(){
cout << "Witaj w programie do liczenia sredniej!\n\n";
while (true){
while (cin >> num){ //stops when you input a char
sum += num;
}
cin.clear();
getline(cin, junk);
cout << "\nSuma tych liczb to: " << sum << "\n\n";
}
return 0;
}
It works this way:
12<ENTER>
24<ENTER>
q<ENTER>
Sum of these numbers is: 36
If something is unclear let me know and I'll try to improve. Any help appreciated :)