Created
July 19, 2009 00:20
-
-
Save allisons/149750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
int main () | |
{ | |
// Declarations | |
string results; | |
string reply; | |
string seen; | |
string name; | |
string line; | |
int age; | |
double score; | |
double average, under18yavg, under18navg, over18yavg, over18navg; | |
int counter, under18y, under18n, over18y, over18n; | |
int youngycount, oldycount, youngncount, oldncount; | |
string inputFileName; | |
ifstream inputFile; | |
cout << "Input file name: "; | |
getline(cin, inputFileName); | |
// Open the input file. | |
inputFile.open(inputFileName.c_str()); // Need .c_str() to convert a C++ string to a C-style string | |
// Check the file opened successfully. | |
if ( ! inputFile.is_open()) | |
{ | |
cout << "Unable to open input file." << endl; | |
cout << "Press enter to continue..."; | |
getline(cin, reply); | |
exit(1); | |
} | |
counter = 0; // Must initialize the counter to zero | |
score= 0; // Must initialize the accumulator to zero. | |
under18y= 0; | |
under18n = 0; | |
over18n = 0; | |
over18y = 0; | |
youngycount = 0; | |
oldycount = 0; | |
youngncount = 0; | |
oldncount = 0; | |
while (inputFile) { | |
inputFile >> name >> seen >> age >> score; | |
cout << setw(17) << name; | |
cout << setw(2) << seen; | |
cout << setw(3) << age; | |
cout << setw(3) << score << endl; | |
if (seen == "Y" && age < 18) | |
{under18y = under18y + score; | |
youngycount++ | |
; | |
} | |
if (seen == "N" && age < 18) | |
{under18n += score; | |
youngncount++; | |
} | |
if (seen == "Y" && age >= 18) | |
{over18y = over18y + score; | |
oldycount++;} | |
else { | |
over18n = over18n + score; | |
oldncount++; | |
}} | |
under18yavg = under18y / youngycount; | |
under18navg = under18n / youngncount; | |
over18yavg = over18y / oldycount; | |
over18navg = over18n / oldncount; | |
average = (under18n + under18y + over18y + over18n) / (youngycount + youngncount + oldycount + oldncount); | |
cout << fixed << showpoint << setprecision(2); | |
cout << "Average score for subjects under 18 who have not seen the ad. "<< under18navg << endl; | |
cout << "Average score for subjects under 18 who have seen the ad. "<< under18yavg << endl; | |
cout << "Average score for subjects 18 and over who have not seen the ad. "<< over18navg << endl; | |
cout << "Average score for subjects 18 and over who have seen the ad. "<< over18navg << endl; | |
cout << "Average score for all subjects. " << average << endl; | |
// Close the input file stream | |
inputFile.close(); | |
cout << "Press enter to continue..."; | |
getline(cin, reply); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment