Skip to content

Instantly share code, notes, and snippets.

@allisons
Created July 19, 2009 00:20
Show Gist options
  • Save allisons/149749 to your computer and use it in GitHub Desktop.
Save allisons/149749 to your computer and use it in GitHub Desktop.
#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;
cout << fixed << showpoint << setprecision(2);
cout << "Total Score for the Under 18 who have seen the ad = " << under18y << " and the avg is "<< under18yavg << endl;
cout << "Total Score for the Under 18 who have not seen the ad = "<< under18n << " and the avg is "<< under18navg << endl;
cout << "Total Score for Adults who have seen the ad = " << over18y << " and the avg is "<< over18yavg << endl;
cout << "Total Score for Adults who have not seen the ad = " << over18n << " and the avg is "<< over18navg << 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