while loop in an if statement

Hi everyone, I just started learning C++ from YouTube a few days ago. I wanted to test what I've learned so far.

I wanted to make a program that asked the user to input the number of people in their family, then ask if what they entered was correct. Then eventually I would tell the user the average age of the family. So here's my problem...

int fam, yon, age, agetot, stopper ;
char Y, y;
agetot =0;
stopper=0;
cout << "This program will determine your family's average age.\n";
cout << "How many people are in your family?\n";
cin >> fam ;
cout << "There are " << fam << " people in your family. Is this correct? Press Y for yes, N for no.\n";
cin >> yon;
if ((yon==Y) || (yon==y));{
cout << "Enter a family member's age, then press enter.\n";
}
while (stopper <= fam){
cin >> age;
agetot= agetot + age;
stopper++;
};
cout << "Your family's age total is " << agetot << endl;


Everything works fine up until the point where the user is supposed to enter the family member's age. Then it wont let anything be typed in and outputs "Your family's age total is 0."
Whats the problem here? Thanks for all help.
Last edited on
while (age <= fam)

Have you initialized age with any value before you try to compare it with fam (which I'm guessing has a user input value already?)

Also - I'm not sure that makes sense logically. If fam is the number of family members, let's say 4, would you want to compare someone's age with the number of people in the family?
my mistake, I pasted the wrong code. take another look!
yon is an int and your comparing that to char y and Y both of which are uninitialized. I think this is what you intend to do
1
2
char yon; 
if(yon == 'Y'|| yon == 'y')


Also since stopper is starting from 0 you need to put stopper < fam (or make stopper = 1)

Oh also use code tags makes stuff eaiser to read
[code]
//code
[/code]
Last edited on


if ((yon=='Y') || (yon=='y')); <- remove the semicolon here

You may need to clear the cin buffer between receiving the char input and the int input of the ages.
1
2
	cin.clear();
	cin.ignore();


What if they say the number they entered is not correct?


Edit: If you edit your post, highlight your code, then click on the <> button in the format palette on the right side, your code will format on the forum.
Last edited on
As a matter of interest what is wrong with leaving the semicolon here
if ((yon=='Y') || (yon=='y')); (or after any loop declaration) ?

I know your not supposed to do it but the code will still compile fine.
Last edited on
It's not a compiler error but it would probably cause a logic error. That ends the if condition right there. Presumably you want the next line or block of code {} to run only if the if condition is met. With the if statement ended early, the next line or block of code would run regardless of the condition in the if.


1
2
if ((yon=='Y') || (yon=='y'))
; // do nothing  


Last edited on
thanks wildblue
[\facepalm]
wow what i asked was a stupid question...i wasn't thinking straight.
So what would I declare yon as?
And I didn't get to the last part yet...if the answer was no. Because I ran across this problem
I'd agree with Void life - yon can be a char.

And OK, I wasn't sure if you just hadn't gotten to that part yet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int fam, yon, age, agetot, stopper ;
	char Y, y;

	cout << "This program will determine your family's average age.\n";
	cout << "How many people are in your family?\n";
	cin >> fam ;
	cout << "There are " << fam << " people in your family. Is this correct? Press Y for yes, N for no.\n";
	cin >> yon;
	if ((yon==Y) || (yon==y)){
	  cout << "Enter a family member's age, then press enter.\n";
        agetot =0;
        stopper=1;
            while (stopper <= fam){
            cin >> age;
            agetot= agetot + age;
            stopper++;
            }};
    cout << "Your family's age total is " << agetot << endl;
:( still doesn't work. It outputs a large seemingly random number
line 1 - don't declare yon as an int
line 2 - you don't need to declare y and Y as char - this is where you want to declare yon as a char

line 9 - you want to see if yon has a value of 'y' or 'Y' - you need the single quotes around the character there.

You may need to clear the cin buffer between receiving the char input and the int input of the ages.
 
cin.ignore();

Last edited on
Wildblue, thank you bro, you're the best! Now that I'm thinking about it, it makes a lot more sense that way. And there was no need to clear the cin buffer. I appreciate your help immensely!
Also thank you void life. Shoulda listened to you the first time around!
Topic archived. No new replies allowed.