fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. float numb; float sum=0;
  8.  
  9. int main()
  10. {
  11. cout << "This app calculates the sum of all entered numbers." << endl;
  12. cout << "To stop the program, enter 0." << endl << endl;
  13. cout << "Enter the first number: ";
  14.  
  15. string input;
  16.  
  17. while(cin >> input)
  18. {
  19. stringstream sst(input);
  20. if (sst>>numb) {
  21. sum += numb;
  22.  
  23. cout << "Sum equals: " << sum << endl << endl;
  24.  
  25. if (numb==0)
  26. {
  27. cout << "Entered 0." << endl;
  28. break; // exits the while loop
  29. }
  30. cout << "Enter another number: ";
  31. }
  32. else
  33. {
  34. cout << "Ignored entry "<<input<<endl;
  35. }
  36. }
  37. cout << "Press Enter to terminate the app." << endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
12 13.5 .2 zee 1.3e02 bee 2 0 5
stdout
This app calculates the sum of all entered numbers.
To stop the program, enter 0.

Enter the first number: Sum equals: 12

Enter another number: Sum equals: 25.5

Enter another number: Sum equals: 25.7

Enter another number: Ignored entry zee
Sum equals: 155.7

Enter another number: Ignored entry bee
Sum equals: 157.7

Enter another number: Sum equals: 157.7

Entered 0.
Press Enter to terminate the app.