Skip to content

Instantly share code, notes, and snippets.

@allisons
Created July 18, 2009 23:40
Show Gist options
  • Save allisons/149737 to your computer and use it in GitHub Desktop.
Save allisons/149737 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;
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 << fixed << showpoint << setprecision(2);
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;
while (inputFile) {
inputFile >> name >> seen >> age >> score;
cout << "Name = " << name << endl;
cout << "Seen = " << seen << endl;
cout << "Age = " << age << endl;
cout << "score = " << score << endl;
if (seen == "y" && age < 18)
{under18y = under18y + score;
youngycount++;
cout << "Under 18 y count = " << youngycount << endl; }
if (seen == "n" && age < 18)
{under18n += score;
youngncount++;}
if (seen == "y" && age >= 18)
{over18y = over18y + score;
oldycount++;}
else {
over18n = over18n + score;
oldncount++;
}
cout << "Total Score for the Under 18 who have seen the ad = " << under18y << endl;
cout << "Total Score for the Under 18 who have not seen the ad = "<< under18n << endl;
cout << "Total Score for Adults who have seen the ad = " << over18y << endl;
cout << "Total Score for Adults who have not seen the ad = " << over18n << 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