Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream> //file functions
- #include <iostream> //cin, cout functions
- #include <iomanip> //setw, setprecision functions
- #include <cstdlib> //exit function, EXIT_FAILURE
- using namespace std; //location of classes and functions
- // defined in standard libraries
- const char IN_FILE[] = "lab12.dat";
- const char OUT_FILE[] = "Chili.txt";
- // function prototypes
- void OpenInputFile (ifstream& /* dataFileIn */);
- void OpenOutputFile (ofstream& /* dataFileOut */);
- void ReadAndProcessFiles (ifstream& /*dataFileIn*/,
- ofstream& /*dataFileOut*/);
- void PrintHeading (ofstream& /*dataFileOut*/);
- string rate;
- string rating (double, double, double);
- int main ()
- {
- ifstream dataFileIn; //input file stream
- ofstream dataFileOut; //output file stream
- OpenInputFile(dataFileIn); //terminate program if open fails
- OpenOutputFile(dataFileOut); //terminate program if open fails
- //format output settings
- dataFileOut << setprecision(2) << fixed;
- //Print Heading for output file
- PrintHeading(dataFileOut);
- //Read from input file; write to output file
- ReadAndProcessFiles(dataFileIn,dataFileOut);
- dataFileIn.close (); //close input file
- dataFileOut.close(); //close output file
- return 0;
- } //end main
- //....function definitions...................
- void OpenInputFile (ifstream& dataFileIn)
- { //Purpose: open file and test for successful open
- //PreCondition: None
- //PostCondition: file successfully opened or program terminated
- // on failure
- dataFileIn.open(IN_FILE); //open input file
- if ( !dataFileIn ) //test for open failure
- { cout << "*** Error: cannot open file *** "<< IN_FILE << endl;
- exit(EXIT_FAILURE); //open failure; terminate program
- }
- }//end OpenInputFile
- void OpenOutputFile (ofstream& dataFileOut)
- { //Purpose: open file and test for successful open
- //PreCondition: None
- //PostCondition: file successfully opened or program terminated
- // on failure
- dataFileOut.open(OUT_FILE); //open output file
- if ( !dataFileOut ) //test for open failure
- { cout << "*** Error: cannot open file *** "<< OUT_FILE << endl;
- exit(EXIT_FAILURE); //open failure; terminate program
- }
- }//end OpenOutputFile
- void PrintHeading(ofstream& dataFileOut)
- { //Purpose: print a heading to an output file
- //PreCondition: output file has been successfully opened
- //PostCondition: heading has been printed
- dataFileOut << "CHILI COOK OFF RANKING SOFTWARE" << endl;
- dataFileOut << "======================================================\n" ;
- dataFileOut << "Team Name J1 J2 J3 AVG RANKING\n";
- return;
- }// end PrintHeading
- // THIS IS WHERE I NEED HELP - I NEED TO AVERAGE THE SCORES ETC...
- // and display, team name, score1,2,3, average and ranking.
- // i need to take in the 3 scores, int score1, score2, etc... then avg those
- // and put into that if statement at the bottom
- void ReadAndProcessFiles(ifstream& dataFileIn,
- ofstream& dataFileOut)
- { //Purpose: read from an input file, write to an output file
- // File Format: XXXXXXXXXXXXXX_999
- //PreCondition: both files have been successfully opened
- //Postcondition: the output file is created with data from
- // the input file
- char team[20]; //team name
- int score; //ID number
- double s1,s2,s3;
- double avg;
- int num = 0;
- int sum = 0;
- dataFileIn.get(team,20); //read 14 char + '\0'; priming read
- while ( !dataFileIn.eof() ) //while not end-of-file
- {
- dataFileIn >> team;
- dataFileIn.ignore();
- dataFileIn >> s1;
- dataFileIn.ignore();
- dataFileIn >> s2;
- dataFileIn.ignore();
- dataFileIn >> s3;
- dataFileIn.ignore(); //consume '\n' char //consume '\n' char
- rating (s1, s2, s3);
- dataFileOut << left << setw(20)<< team
- << setw(5) << s1 << setw(5) << s2 << setw(5) << s3 << " "<< setw(7) << ((s1 +s2 +s3)/3) << setw(5) << rate << endl ;
- dataFileIn.get(team,30); //read 14 char + '\0'
- } //end while
- }//end ReadAndProcessFiles
- string rating (double s1, double s2, double s3)
- {
- string str;
- string rate;
- string rate1 = "Novice";
- string rate2 = "Good";
- string rate3 = "Strong";
- string rate4 = "Superior";
- double avg;
- avg = (s1 + s2 + s3) / 3;
- fflush(stdin);
- if (1 < avg && avg <= 3) {
- rate = rate1;
- }
- else if (3 < avg && avg <= 5) {
- rate = rate2;
- }
- else if (5 < avg && avg <=8) {
- rate = rate3;
- }
- else if (8 > avg) {
- rate = rate4;
- }
- return rate;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement