Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <cstdlib> //needed for exit function
- using namespace std;
- int fileSum(string filename);
- int main() {
- string filename;
- cout << "Enter the name of the input file: ";
- cin >> filename;
- cout << endl;
- cout << "Sum: " << fileSum(filename) << endl;
- return 0;
- }
- int fileSum(string filename){
- ifstream obj;
- obj.open(filename);
- int sum = 0;
- if(!obj){
- cout<<"Error opening " << filename <<endl;
- exit(1);
- }
- else{
- int value;
- while(obj >>value){
- sum = sum + value;
- }
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement