Johnthomas

Untitled

Nov 5th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<string>
  6. #include<iomanip>
  7.  
  8. using namespace std;
  9.  
  10. void openInput(ifstream&);
  11. void openOutput(ofstream&);
  12. void average_score(int&, ofstream&);
  13. void file_update(ifstream&, ofstream&);
  14. int main()
  15. {
  16.     ifstream inscore;
  17.     ofstream outscore;
  18.    
  19.     openInput(inscore);
  20.     openOutput(outscore);
  21.     file_update(inscore, outscore);
  22.    
  23.     return 0;
  24.  
  25. }
  26.  
  27. void openInput(ifstream& fname)
  28. {
  29.     string filename;
  30.     cout << "\nPlease enter the file filename you would like to open: ";
  31.     cin >> filename;
  32.     fname.open(filename.c_str());
  33.    
  34.     if (fname.fail())
  35.     {
  36.         cout << "\nFailed to open file named " << filename << " for input"  <<  endl;
  37.     exit(1);
  38.     }
  39.     return;
  40. }
  41. void openOutput(ofstream& fname)
  42. {
  43.     string filename;
  44.     cout << "\nPlease enter the file filename you would like to output: ";
  45.     cin >> filename;
  46.     fname.open(filename.c_str());
  47.    
  48.     if (fname.fail())
  49.     {
  50.         cout << "\nFailed to open file named " << filename << " for output" << endl;
  51.     exit(1);
  52.     }
  53.    
  54.  
  55. }
  56. void file_update(ifstream& inscore, ofstream& outscore)
  57. {
  58.     string lastname,firstname;
  59.     int  subtotal=0, score=0;
  60.  
  61.     while(inscore.good())
  62.     {
  63.         inscore >> lastname;
  64.         inscore >> firstname;
  65.         outscore << lastname << " " << firstname << " ";
  66.  
  67.         for(int count=0;count<10;count++)
  68.         {
  69.             inscore >> score;
  70.             outscore << score << " ";
  71.             subtotal = score + subtotal;
  72.         }
  73.         average_score(subtotal, outscore);
  74.         outscore << "\r\n";
  75.         subtotal=0;
  76.     }
  77.  
  78.     inscore.close();
  79.     outscore.close();
  80. }
  81.  
  82. void average_score(int& subtotal, ofstream& outscore)
  83. {
  84.     double average = (subtotal / 10.00);
  85.     outscore << fixed << setprecision(2) << average << endl;
  86.     return;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment