Hi all.
I'm supposed to write a program that reads info (name, gender, school, and test scores) from a file and outputs the average test scores of different groups (males, university students, etc.).
I've done a lot of reading but am still pretty stumped in a lot of ways.
I know I need to use a while loop so the program keeps reading data until the end of the file, but I don't know how set it up so the program saves the data as the appropriate type and can read several lines of data without having a variable for each piece of data. Keeping to beginner functions. Only in Ch 5 of D.S. Malik C++ Programming
For example, if I have a line in the input file like this:
Tabatha F CC 75
I need to be able to use that score of 75 to figure out average scores for all females.
I know I'm not giving you a lot to go on, but any help you could give me would be greatly appreciated. Thank you.
Here is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int counter;
string schoolType;
char sex;
double score;
double aveCC, aveUN;
double totalCC, totalUN;
double sumCC, sumUN;
double totalScore, highScore, averageScore;
string line;
string inputPathName;
ifstream inputFile;
ifstream dataIn;
int countUN, countCC;
string studentName;
do {
cout << "Please enter input path name: ";
cin >> inputPathName;
dataIn.open(inputPathName.c_str());
if (!dataIn.is_open()) {
cout << "File open failed" << endl;
}
} while (!dataIn.is_open());
while (getline(dataIn, line)) {
cout << line << endl;
counter = 0;
counter++;
}
while (!inputFile.eof()){
//inputFile >> city >> state >> price;
inputFile >> sex >> schoolType >> score;
if (schoolType == "CC"){
aveCC = totalCC / sumCC;
countCC++;
}
else if (schoolType == "UN"){
aveUN = totalUN / sumUN;
countUN++;
}
else cout << "Invalid State";
}
cout << "There are " << counter << " students in the class" << endl;
if (counter >= 1) {
averageScore = (totalScore/counter);
|