-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.cpp
42 lines (34 loc) · 1.01 KB
/
11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main() {
std::string ifName;
std::cout << "Please enter the input file name: ";
std::cin >> ifName;
std::ifstream inFile(ifName);
if (!inFile.is_open()) {
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}
int sum = 0;
std::string line;
while (std::getline(inFile, line)) {
std::istringstream lineStream(line);
std::string word;
while (lineStream >> word) {
try {
int number = std::stoi(word);
sum += number;
}
catch (const std::invalid_argument& e) {
}
catch (const std::out_of_range& e) {
std::cerr << "Integer is out of range: " << word << std::endl;
}
}
}
inFile.close();
std::cout << "The sum of all integers in that file is: " << sum << std::endl;
return 0;
}