-
-
Save matasar/149715 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 reply; | |
string blanks; | |
string name; | |
char seen; | |
int age; | |
double score; | |
// double average, under18yavg, under18navg, over18yavg, over18navg; | |
int counter, under18y, under18n, over18y, over18n; | |
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.peek() != EOF) { | |
getline(inputFile, name); | |
cout << name << endl; | |
} | |
cout << "\nEnd of file reached\n" << endl; | |
{do { | |
inputFile.ignore (18); | |
inputFile.get(seen); inputFile.ignore (1); inputFile >> age; inputFile.ignore (1); inputFile >> score; | |
if (seen == 'y' && age < 18) | |
do {under18y = under18y + score; | |
counter++; | |
cout << under18y << endl;} | |
while (inputFile.peek() != EOF); | |
if (seen == 'n' && age < 18) | |
do {under18n = under18n + score; | |
counter++; | |
cout << under18n << endl;} | |
while (inputFile.peek() != EOF); | |
if (seen == 'y' && age >= 18) | |
do {over18y = over18y + score; | |
counter++; | |
cout << over18y << endl;} | |
while (inputFile.peek() != EOF); | |
else { do { | |
over18n = over18n + score; | |
counter++; | |
cout << over18n << endl;} | |
while (inputFile.peek() != EOF);}} | |
while (inputFile.peek() != EOF); | |
cout << "under 18 no score" << under18n << "under 18 y score" << under18y << "Adult n score" << over18n << "adult yes score" << over18y; | |
// 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