Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- using namespace std;
- int main()
- {
- //Define the Variables
- string Firstname, Gender, College, path;
- string filename = "scores.txt";
- float Average = 0, Score = 0, TotalUN = 0, CountUN = 0, TotalCC = 0, CountCC = 0, TotalM = 0, CountM = 0, TotalF = 0, CountF = 0;
- fstream infile;
- cout << "Please input the path to the file scores.txt: ";
- cin >> path;
- string fullpath = path + filename;
- //open the file
- infile.open(fullpath);
- //If an error occurs
- if (!infile)
- {
- cout << "Cannot open the file at the target location."
- << "The program will now terminate" << endl;
- exit;
- }
- cout << "This is the data within the specified file: " << endl;
- while (!infile.eof())
- {
- infile >> Firstname >> Gender >> College >> Score;
- cout << fixed << showpoint << setprecision(2);
- cout << setw(10) << left << Firstname
- << setw(10) << left << Gender
- << setw(10) << left << College
- << setw(8) << Score << endl;
- }
- cout << "Here are the averages: " << endl;
- if (College == "UN")
- {
- TotalUN = TotalUN + Score;
- CountUN++;
- }
- cout << fixed << showpoint << setprecision(2);
- cout << "Average of University students = " << TotalUN / CountUN << endl;
- if (College == "CC")
- {
- TotalCC = TotalCC + Score;
- CountCC++;
- }
- cout << fixed << showpoint << setprecision(2);
- cout << "Average of Community College students = " << TotalCC / CountCC << endl;
- if (Gender == "M")
- {
- TotalM = TotalM + Score;
- CountM++;
- }
- cout << fixed << showpoint << setprecision(2);
- cout << "Average of Male students = " << TotalM / CountM << endl;
- if (Gender == "F")
- {
- TotalF = TotalF + Score;
- CountF++;
- }
- cout << fixed << showpoint << setprecision(2);
- cout << "Average of Female students = " << TotalF / CountF << endl;
- infile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement