I have an assignment like this:
Your assignment is to prompt the user to enter the full pathname to the data file on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.
The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.
Your program should then calculate and display the the following results:
Average score for subjects under 18 who have not seen the ad.
Average score for subjects under 18 who have seen the ad.
Average score for subjects 18 and over who have not seen the ad.
Average score for subjects 18 and over who have seen the ad.
Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.
Finally, your program should calculate and display to two places of decimals the overall average score for all of the subjects surveyed. (Note: Mathematically, this is NOT the average of the four averages you calculated previously).
So far, I got this echo, but I don't know how to define under 18, over 18, and taking survey or not.
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// variables
string path;
string reply;
int score1, score2, score3, score4;
int age = 0;
int adScore = 0;
int notAdScore = 0;
// open file
ifstream inFile;
// prompt and enter the file name
cout << "Please enter the file following the format C:/name: ";
getline(cin, path);
// open the path
inFile.open(path.c_str());
// if file not exist
if ( ! inFile.is_open()) {
cout << "The file does not exist. Please press any key to quit! ";
getline(cin, reply);
exit(1);
}
// if exist, read the file
while (!inFile.eof()){
char c;
inFile.get(c);
cout << c;
}
// restore the file
inFile.clear();
inFile.seekg(0);
cout << endl << endl;
// pause and exit
getchar();
getchar();
return 0;
}
|