fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int limit = 5;
  6. int sum = 0;
  7. char c;
  8. int d;
  9.  
  10. while(limit --> 0) {
  11. if (cin >> c) {
  12. d = c - '0';
  13. if (d > 9 || d < 0) {
  14. cout << "Wrong digit: " << c << endl;
  15. } else {
  16. sum += d;
  17. }
  18. } else {
  19. break;
  20. }
  21. }
  22.  
  23. cout << "Sum: " << sum << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 4524KB
stdin
110x0
stdout
Wrong digit: x
Sum: 2