fork download
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout << "please enter numbers to add seperated by a '+'\n"
  6. "and end with a zero. for example: 12.6 + 5.4 + 9.2 + 0\n? " ;
  7.  
  8. float sum = 0.0 ;
  9. float number ;
  10. char c ;
  11.  
  12. while( std::cin >> number && number != 0 && std::cin >> c && c == '+' )
  13. sum += number ;
  14.  
  15. std::cout << "sum is: " << sum << '\n' ;
  16. }
  17.  
Success #stdin #stdout 0s 3432KB
stdin
12.6 + 5.4 + 9.2 + 0.0
stdout
please enter numbers to add seperated by a '+'
and end with a zero. for example: 12.6 + 5.4 + 9.2 + 0
? sum is: 27.2