fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6. #include <stdexcept>
  7. using namespace std;
  8.  
  9. inline void keep_window_open() { /*char ch; cin >> ch;*/ }
  10.  
  11. int main() {
  12. int sum = 0, number;
  13. vector<int> numbers;
  14. string word;
  15. //ifstream file("file1.txt");
  16. istream &file = cin;
  17.  
  18. while (file >> word) {
  19. try {
  20. number = stoi(word);
  21. }
  22. catch (const std::exception &) {
  23. continue;
  24. }
  25. numbers.push_back(number);
  26. }
  27.  
  28. if (file.fail() && !file.eof())
  29. {
  30. cout << "failed\n";
  31. exit(1);
  32. }
  33.  
  34. for (int x : numbers)
  35. sum += x;
  36.  
  37. cout << sum << "\n";
  38. keep_window_open();
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 4380KB
stdin
words 32 jd: 5 alkj 3 12 fjkl 
23 / 32
hey 332 2 jk 23 k 23 ksdl 3
32 kjd 3 klj 332 c 32
stdout
889