Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. //constants
  9. const int ARRAY_MAX = 100;
  10. const int FNAME_LENGTH = 50;
  11.  
  12. //functions
  13. void getFilename(char[], bool);
  14. void openIFile(ifstream&, char[]);
  15. void openOFile(ofstream&, char[]);
  16. void processData(ifstream&, ofstream&);
  17.  
  18. int main()
  19. {
  20.   //define variables
  21.   char ifilename[FNAME_LENGTH];
  22.   char ofilename[FNAME_LENGTH];
  23.   ifstream in;
  24.   ofstream out;
  25.  
  26.   //open files
  27.   getFilename(ifilename, false);
  28.   openIFile(in, ifilename);
  29.   getFilename(ofilename, true);
  30.   openOFile(out, ofilename);
  31.  
  32.   //process files
  33.   processData(in, out);
  34.  
  35.   //close files
  36.   in.close();
  37.   out.close();
  38.  
  39.   system("PAUSE");
  40.   return 0;
  41. }
  42.  
  43. //gets the filename from the user
  44. void getFilename(char out[], bool io)
  45. {
  46.   cout << "Please enter the ";
  47.   if (io)
  48.     cout << "output ";
  49.   else
  50.     cout << "input ";
  51.   cout << "file name: ";
  52.   cin >> out;
  53. }
  54.  
  55. //opens a file for input
  56. void openIFile(ifstream& file , char fname[])
  57. {
  58.   file.open(fname);
  59.   if (file.fail()) {
  60.     cout << "Input file failed to open" << endl;
  61.     exit(-1);
  62.   }
  63. }
  64.  
  65. //opens a file for output
  66. void openOFile(ofstream& file , char fname[])
  67. {
  68.   file.open(fname);
  69.   if (file.fail()) {
  70.     cout << "Output file failed to open" << endl;
  71.     exit(-1);
  72.   }
  73. }
  74.  
  75. //processes the file in into out
  76. void processData(ifstream& in, ofstream& out)
  77. {
  78.   int tmp, tmp2, species, total, count, totalAll, countAll;
  79.   string tmpStr;
  80.  
  81.   in >> tmp;
  82.   in.ignore();
  83.   out << "Year - " << tmp << endl << endl;
  84.   in >> species;
  85.   in.ignore();
  86.   out << "Species - " << "Count: " << species << endl;
  87.   totalAll = 0;
  88.   countAll = 0;
  89.   for (int i = 0; i < species; i++) {
  90.     getline(in, tmpStr);
  91.     out << tmpStr << ": " << endl;
  92.     in >> count;
  93.     in.ignore();
  94.     countAll += count;
  95.     out << "  Populations:";
  96.     total = 0;
  97.     for (int j = 0; j < count; j++) {
  98.       in >> tmp2;
  99.       out << " " << tmp2;
  100.       total += tmp2;
  101.       totalAll += tmp2;
  102.     }
  103.     in.ignore();
  104.     out << endl;
  105.     out << "  Total:       " << total << endl;
  106.     out << "  Average:     " << (total / (double)count) << endl;
  107.   }
  108.   out << endl;
  109.   out << "Total:   " << totalAll << endl;
  110.   out << "Average: " << (totalAll / (double)countAll) << endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement