Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1.  
  2.  
  3. #include <fstream>    //file functions
  4. #include <iostream>   //cin, cout functions
  5. #include <iomanip>    //setw, setprecision functions
  6. #include <cstdlib>    //exit function, EXIT_FAILURE
  7. using namespace std;  //location of classes and functions
  8.                       // defined in standard libraries
  9.  
  10. const char IN_FILE[]  = "lab12.dat";  
  11. const char OUT_FILE[] = "Chili.txt";
  12.  
  13. // function prototypes
  14. void OpenInputFile  (ifstream& /* dataFileIn */);
  15. void OpenOutputFile (ofstream& /* dataFileOut */);
  16. void ReadAndProcessFiles (ifstream& /*dataFileIn*/,
  17.                           ofstream& /*dataFileOut*/);
  18. void PrintHeading (ofstream& /*dataFileOut*/);
  19. string rate;
  20. string rating (double, double, double);
  21. int main ()
  22. {
  23.    
  24.  
  25.   ifstream dataFileIn;      //input file stream
  26.   ofstream dataFileOut;     //output file stream
  27.  
  28.   OpenInputFile(dataFileIn);      //terminate program if open fails
  29.   OpenOutputFile(dataFileOut);    //terminate program if open fails
  30.  
  31.  
  32.  
  33.   //format output settings
  34.   dataFileOut << setprecision(2) << fixed;
  35.  
  36.   //Print Heading for output file
  37.   PrintHeading(dataFileOut);
  38.  
  39.   //Read from input file; write to output file
  40.   ReadAndProcessFiles(dataFileIn,dataFileOut);
  41.  
  42.   dataFileIn.close ();                      //close input file
  43.   dataFileOut.close();                      //close output file
  44.   return 0;
  45. } //end main
  46.  
  47. //....function definitions...................
  48. void OpenInputFile (ifstream& dataFileIn)
  49. { //Purpose:       open file and test for successful open
  50.   //PreCondition:  None
  51.   //PostCondition: file successfully opened or program terminated
  52.   //                on failure
  53.  
  54.   dataFileIn.open(IN_FILE);         //open input file
  55.   if ( !dataFileIn )               //test for open failure
  56.   { cout << "*** Error: cannot open file *** "<< IN_FILE << endl;
  57.     exit(EXIT_FAILURE);      //open failure; terminate program
  58.   }
  59. }//end OpenInputFile
  60.  
  61. void OpenOutputFile (ofstream& dataFileOut)
  62. { //Purpose:       open file and test for successful open
  63.   //PreCondition:  None
  64.   //PostCondition: file successfully opened or program terminated
  65.   //                on failure
  66.  
  67.   dataFileOut.open(OUT_FILE);       //open output file
  68.   if ( !dataFileOut )              //test for open failure
  69.   { cout << "*** Error: cannot open file *** "<< OUT_FILE << endl;
  70.     exit(EXIT_FAILURE);      //open failure; terminate program
  71.   }
  72. }//end OpenOutputFile
  73.  
  74. void PrintHeading(ofstream& dataFileOut)
  75. { //Purpose:       print a heading to an output file
  76.   //PreCondition:  output file has been successfully opened
  77.   //PostCondition: heading has been printed
  78.  
  79.   dataFileOut << "CHILI COOK OFF RANKING SOFTWARE" << endl;
  80.   dataFileOut << "======================================================\n" ;
  81.   dataFileOut << "Team Name            J1   J2   J3    AVG        RANKING\n";
  82.   return;
  83. }// end PrintHeading
  84.  
  85.  
  86. // THIS IS WHERE I NEED HELP - I NEED TO AVERAGE THE SCORES ETC...
  87. // and display, team name, score1,2,3, average and ranking.
  88. // i need to take in the 3 scores, int score1, score2, etc... then avg those
  89. // and put into that if statement at the bottom
  90.  
  91. void ReadAndProcessFiles(ifstream& dataFileIn,
  92.                          ofstream& dataFileOut)
  93. { //Purpose:       read from an input file, write to an output file
  94.   //               File Format: XXXXXXXXXXXXXX_999
  95.   //PreCondition:  both files have been successfully opened
  96.   //Postcondition: the output file is created with data from
  97.   //                the input file
  98.  
  99.   char team[20];     //team name
  100.   int score;      //ID number
  101.   double s1,s2,s3;
  102.   double avg;
  103.   int num = 0;
  104.   int sum = 0;
  105.  
  106.   dataFileIn.get(team,20);       //read 14 char + '\0'; priming read
  107.   while ( !dataFileIn.eof() )       //while not end-of-file
  108.   {  
  109.      dataFileIn >> team;
  110.      dataFileIn.ignore();
  111.      dataFileIn >> s1;        
  112.      dataFileIn.ignore();  
  113.      dataFileIn >> s2;      
  114.      dataFileIn.ignore();
  115.      dataFileIn >> s3;      
  116.      dataFileIn.ignore();           //consume '\n' char          //consume '\n' char        
  117.  
  118.      rating (s1, s2, s3);
  119.  
  120.  
  121.      
  122.      dataFileOut << left << setw(20)<< team
  123.                  << setw(5) << s1 << setw(5) << s2 << setw(5) << s3 << "  "<< setw(7) << ((s1 +s2 +s3)/3) << setw(5) << rate << endl ;  
  124.      dataFileIn.get(team,30);                 //read 14 char + '\0'
  125.   } //end while
  126. }//end ReadAndProcessFiles
  127.  
  128. string rating (double s1, double s2, double s3)
  129. {  
  130.         string str;
  131.         string rate;
  132.         string rate1 = "Novice";
  133.         string rate2 = "Good";
  134.         string rate3 = "Strong";
  135.         string rate4 = "Superior";
  136.         double avg;
  137.  
  138.  
  139.                 avg = (s1 + s2 + s3) / 3;
  140.                 fflush(stdin);
  141.          
  142.                 if (1 < avg && avg <= 3) {
  143.                     rate = rate1;
  144.             }    
  145.            
  146.             else if (3 < avg && avg <= 5) {
  147.                     rate = rate2;
  148.             }
  149.  
  150.                     else if (5 < avg && avg <=8) {
  151.                         rate = rate3;
  152.             }        
  153.                    
  154.             else if (8 > avg) {
  155.                         rate = rate4;
  156.             }  
  157. return rate;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement