Skip to content

Instantly share code, notes, and snippets.

@NickersF
Created October 22, 2015 21:08
Show Gist options
  • Save NickersF/99f3b4b07a763cb7db02 to your computer and use it in GitHub Desktop.
Save NickersF/99f3b4b07a763cb7db02 to your computer and use it in GitHub Desktop.
// Assignment 4: With updated output labels
// Author: Nicholas Fazzolari
// Date: 10/19/2015
// Sources: ** Course Material **
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const string MALE_AVERAGE_LABEL = "The average score for male students is: ";
const string FEMALE_AVERAGE_LABEL = "The average score for female students is: ";
const string CC_AVERAGE_LABEL = "The average score for Community College students is: ";
const string UN_AVERAGE_LABEL = "The average score for University students is: ";
const string TOTAL_AVERAGE_LABEL = "The average test score for all students is: ";
int main()
{
// String variables for caputring input from the user and file data
string reply;
string inputFileName;
string line;
string collegeType;
string genders;
string names;
// Input file data
ifstream inputFile;
// Counter variables will be used a divisors for averages
int maleCount = 0;
int femaleCount = 0;
int ccCount = 0;
int unCount = 0;
int gradeCount = 0;
// Output label length variables
int maleLabelSize = strlen(MALE_AVERAGE_LABEL.c_str());
int femaleLabelSize = strlen(FEMALE_AVERAGE_LABEL.c_str());
int ccLabelSize = strlen(CC_AVERAGE_LABEL.c_str());
int unLabelSize = strlen(UN_AVERAGE_LABEL.c_str());
int avgLabelSize = strlen(TOTAL_AVERAGE_LABEL.c_str());
// Grades for all students
double totalScores;
double totalScoreSum = 0;
// Grades for all CC students
double ccScoreSum = 0;
// Grades for all UN students
double unScoreSum = 0;
// Grades for all male students
double maleScoreSum = 0;
// Grades for all female students
double femaleScoreSum = 0;
cout << fixed << showpoint << setprecision(2);
cout << "Please enter the file name and path: ";
getline(cin, inputFileName);
// Open the input file
inputFile.open(inputFileName.c_str());
// Check if the file opened succuessfully, if not terminate with code 1
// else echo the file to the console window
if (!inputFile.is_open())
{
cout << "Unable to open input file..." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
// Echo the file to the screen
while (inputFile.peek() != EOF)
{
getline(inputFile, line);
cout << line << endl;
}
// Clear the EOF flag in order to rewind and re-read the data from the beginning
inputFile.clear();
// 'Rewind' the the ifstram to byte 0
inputFile.seekg(0);
// Now read the file one token at a time into discrete variables
inputFile >> names >> genders >> collegeType;
inputFile >> totalScores;
// Loop to read the totalScores for all students
while (inputFile)
{
totalScoreSum = totalScoreSum + totalScores;
gradeCount++;
// Retrieve data for the genders
if (genders == "M")
{
maleScoreSum = maleScoreSum + totalScores;
maleCount++;
}
else
{
femaleScoreSum = femaleScoreSum + totalScores;
femaleCount++;
}
// Retrieve data by college type
if (collegeType == "CC")
{
ccScoreSum = ccScoreSum + totalScores;
ccCount++;
}
else
{
unScoreSum = unScoreSum + totalScores;
unCount++;
}
// Continue to read the file one token at a time into discrete variables
inputFile >> names >> genders >> collegeType;
inputFile >> totalScores;
} // End while
// Print the counters of the tokens read or print no data message.
if (maleCount != 0 && femaleCount != 0 && ccCount != 0 && unCount != 0 && gradeCount != 0)
{
cout << "\n**** AVERAGES FOR DIFFERENT GROUPS LISTED BELOW ****\n";
cout << setfill('.') << endl;
cout << MALE_AVERAGE_LABEL << setw(64 - maleLabelSize) << maleScoreSum / maleCount << endl;
cout << endl;
cout << FEMALE_AVERAGE_LABEL << setw(64 - femaleLabelSize) << femaleScoreSum / femaleCount << endl;
cout << endl;
cout << CC_AVERAGE_LABEL << setw(64 - ccLabelSize) << ccScoreSum / ccCount << endl;
cout << endl;
cout << UN_AVERAGE_LABEL << setw(64 - unLabelSize) << unScoreSum / unCount << endl;
cout << endl;
cout << TOTAL_AVERAGE_LABEL << setw(64 - avgLabelSize) << totalScoreSum / gradeCount << endl;
cout << setfill(' ') <<endl;
}
else
{
cout << "No data found. Closing file stream..." << endl;
}
// Close the input file stream
inputFile.close();
// Exit prompt
cout << "Press enter to continue...";
getline(cin, reply);
return 0;
}
// Assignment 4: With unformatted output labels
// Author: Nicholas Fazzolari
// Date: 10/19/2015
// Sources: ** Course Material **
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// String variables for caputring input from the user and file data
string reply;
string inputFileName;
string line;
string collegeType;
string genders;
string names;
// Input file data
ifstream inputFile;
// Counter variables will be used a divisors for averages
int maleCount = 0;
int femaleCount = 0;
int ccCount = 0;
int unCount = 0;
int gradeCount = 0;
// Grades for all students
double totalScores;
double totalScoreSum = 0;
// Grades for all CC students
double ccScoreSum = 0;
// Grades for all UN students
double unScoreSum = 0;
// Grades for all male students
double maleScoreSum = 0;
// Grades for all female students
double femaleScoreSum = 0;
cout << fixed << showpoint << setprecision(2);
cout << "Please enter the file name and path: ";
getline(cin, inputFileName);
// Open the input file
inputFile.open(inputFileName.c_str());
// Check if the file opened succuessfully, if not terminate with code 1
// else echo the file to the console window
if (!inputFile.is_open())
{
cout << "Unable to open input file..." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
// Echo the file to the screen
while (inputFile.peek() != EOF)
{
getline(inputFile, line);
cout << line << endl;
}
// Clear the EOF flag in order to rewind and re-read the data from the beginning
inputFile.clear();
// 'Rewind' the the ifstram to byte 0
inputFile.seekg(0);
// Now read the file one token at a time into discrete variables
inputFile >> names >> genders >> collegeType;
inputFile >> totalScores;
// Loop to read the totalScores for all students
while (inputFile)
{
totalScoreSum = totalScoreSum + totalScores;
gradeCount++;
// Retrieve data for the genders
if (genders == "M")
{
maleScoreSum = maleScoreSum + totalScores;
maleCount++;
}
else
{
femaleScoreSum = femaleScoreSum + totalScores;
femaleCount++;
}
// Retrieve data by college type
if (collegeType == "CC")
{
ccScoreSum = ccScoreSum + totalScores;
ccCount++;
}
else
{
unScoreSum = unScoreSum + totalScores;
unCount++;
}
// Continue to read the file one token at a time into discrete variables
inputFile >> names >> genders >> collegeType;
inputFile >> totalScores;
} // End while
// Print the counters of the tokens read or print no data message.
if (maleCount != 0 && femaleCount != 0 && ccCount != 0 && unCount != 0 && gradeCount != 0)
{
cout << "\n**** AVERAGES FOR DIFFERENT GROUPS LISTED BELOW ****\n";
cout << endl;
cout << "The average score for male students is: " << maleScoreSum / maleCount << endl;
cout << endl;
cout << "The average score for female students is: " << femaleScoreSum / femaleCount << endl;
cout << endl;
cout << "The average score for Community College students is: " << ccScoreSum / ccCount << endl;
cout << endl;
cout << "The average score for University students is: " << unScoreSum / unCount << endl;
cout << endl;
cout << "The average test score for all students is: " << totalScoreSum / gradeCount << endl;
cout << endl;
}
else
{
cout << "No data found. Closing file stream..." << endl;
}
// Close the input file stream
inputFile.close();
// Exit prompt
cout << "Press enter to continue...";
getline(cin, reply);
return 0;
}
Mike M CC 88.3
Julie F UN 92
Edgar M CC 98
Hans M CC 77.3
Riley F UN 67
Keith M UN 72
Maggie F CC 82
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment