/ Published in: C++
Reads a line from a specified txt file in format: Name Gender School Score Gender is M/F, school is UN/CC and Score is Percentage.
Expand |
Embed | Plain Text
// FIleReader.cpp : Defines the entry point for the console application. // // Includes - iostream for input/output to console // fstream for reading from the file // string for ease of use string manipulation // iomanip to format outputs to 2 decimal places. #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main( ) { //string declarations string filename; string currentline; //declare the input file stream ifstream scores; //declaration of numbers for tracking scores and # of students int male = 0; int female = 0; int un = 0; int cc = 0; float mtotal = 0; float ftotal = 0; float untotal = 0; float cctotal = 0; int temptotal; //Character declarations to check gender, school, and if the program should continue running. char gender; char school[3]; char cont = ' '; // While continue is not No then keep running the program. while ( cont != 'n' && cont != 'N' ) { //ask user to input location and name of file. - Format should be C:\folder\filename.ext cout << "Please type the location and name of the file to read" << endl; cin >> filename; // Open the file scores.open (filename); //check to make sure the user entered the name of the file properly, and the file could be opened. while ( !scores.is_open() ) { cout << endl << "The file could not be opened properly, please check for proper spelling & location and type again" << endl; cin >> filename; scores.open (filename); } cout << endl; //Once the file is open loop through it getting each line till we read the end. while ( scores.good() ) { //Get the next line set it as the current line and output it. getline (scores, currentline); cout << currentline << endl; //scan the current line for gender, school type, and score, but ignore name. sscanf (currentline.c_str(), "%*s %c %s %i", &gender, &school, &temptotal ); //Add up totals for different categories and increment the number of student of each type found for the current line. if ( gender == 'F' || gender == 'f' ) { female++; ftotal += temptotal; } else { male++; mtotal += temptotal; } if ( strcmp(school, "CC") == 0 ) { cc++; cctotal += temptotal; } else { un++; untotal += temptotal; } } //Set decimal precision to 2 places, then output all required data. cout << endl << fixed << setprecision(2); cout << "Average score of male students: " << mtotal / male << endl; cout << "Average score of female students: " << ftotal / female << endl; cout << "Average score of UN students: " << untotal / un << endl; cout << "Average score of CC sutdents: " << cctotal / cc << endl; cout << "Overall average scores: " << (ftotal + mtotal) / (male + female) << endl; //prompt user to continue the program or exit. cout << "Would you like to continue reading another file? (y/n): "; cin >> cont; cout << endl << endl << endl; } return 0; }
You need to login to post a comment.
