Do I need the "while" in a "while" or something different?

Hello. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace 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.
Last edited on
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.
Last edited on
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
Last edited on
Anyone?
Hello miha2,

your code cannot work since when do you provide the variable y with a value? It remains uinitialized.

miha2 wrote:
P.S. Also, seenAd in the second while doesn't want to work with ==
what do you mean by this? maybe that above

I suspect that you don't want that nested while loops but instead an if?

This while (seenAd = 'y' && age <= 18) is certainly not what you want since it assigns 'y' to 'seenAd' whenever it loops.
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);
		while if((answer[0] == 'y') || (answer[0] == 'Y')  && age <= 18) {
you should make it more usefull though
Topic archived. No new replies allowed.