#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
int main ()
{
int age;
int score;
int total;
int seenAd;
int count = 0;
int y, n;
string name;
string inputFileName;
string reply;
double sum = 0;
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);
}
cout << "This program counts all Yeses and Noes for Adults and children" << endl;
// Just a try
while (inputFile >> name >> seenAd >> age >> score) { // meaning that if answer is Yes and person is younger than 18
while (seenAd == y && age <= 18) {
sum = sum + score;
count++;
cout << "The total for children who has not seen this ad is: " << sum / count;
}
}
cout << "The total average is: " << sum / count << endl;
cout << "Please enter q to quit: ";
cin >> reply;
return 0;
}
Please tell me where I'm wrong. I need to finish it by Sunday. I really need your help. I was told that I need the "while (inputFile >> name...) " statement, but wasn't told about how to use it properly. I think I need the second while statement, but not sure. So, briefly, it should tell (and, actually, show the file, I guess) how many children and adults saw the ad and how many didn't, and count their total. And at the end it should tell to total average. It did before, but not now. Not when I included the "while" statement.
In the line while (seenAd == y && age <= 18) Why is seenAd a character value or it would be if it were 'y' instead of just y? When you declared it above, it is an integer value. Did you mean to convert it before comparing it in the while statement? I don't see why when you are inputing the values into a file.
Change that for your purpose and see if it builds. I hope this helps you fix your problem.
OK, I changed my "seenAd" to be a char, and changed my second while (seenAd = 'y' && age <= 18) { and it did work, though I had to but break in it to stop, or it will write to me endlessly. But even that didn't help me, since it gave me about 6 continuous sentences: "The total of children who saw this message is " and all wrong scores. More ideas are welcome.
P.S. Also, seenAd in the second while doesn't want to work with ==, don't know why
All right, so what it should be assigned to seenAd so that if it's yes, it'd output only answers with yes, if no - only no? Also, should I put one = or 2? Finally, what the y and n should be? an int? a string? a char? something different?
that depends on what you want. if you want input from the user for each line processe then you can write something like that:
1 2 3 4 5
while (inputFile >> name >> seenAd >> age >> score) { // meaning that if answer is Yes and person is younger than 18
std::cout << "Say yes or no";
std::string answer;
getline(std::cin, answer);
whileif((answer[0] == 'y') || (answer[0] == 'Y') && age <= 18) {