Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib> //needed for exit function
  4.  
  5. using namespace std;
  6.  
  7. int fileSum(string filename);
  8.  
  9. int main() {
  10. string filename;
  11. cout << "Enter the name of the input file: ";
  12. cin >> filename;
  13. cout << endl;
  14. cout << "Sum: " << fileSum(filename) << endl;
  15.  
  16. return 0;
  17. }
  18.  
  19. int fileSum(string filename){
  20. ifstream obj;
  21. obj.open(filename);
  22. int sum = 0;
  23. if(!obj){
  24. cout<<"Error opening " << filename <<endl;
  25. exit(1);
  26. }
  27. else{
  28. int value;
  29. while(obj >>value){
  30. sum = sum + value;
  31. }
  32. }
  33. return sum;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement