Having Trouble with C++ assignment

So, I am trying to code this program. Below is my problem and I have wrote code as much as I can. I am almost there, but not quiet there. So, please help me out if you could.

This program is broken down into phases for your convenience only. Please turn in only the final phase. Before turning in your program, please make sure that it does something reasonable if the user enters a negative number the first time.

Enter age of attendee (-1 to quit): 34
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 16
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 68
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 53
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 39
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 23
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 21
Enter gender (M or F): F
Enter age of attendee (-1 to quit): -1

age 0 to 18: 1
age 19 to 30: 2
age 31 to 40: 2
age 41 to 60: 1
over 60: 1

males: 2
females: 5
The average age was 36.
The youngest person in attendance was 16.
The oldest person in attendance was 68.


And here is my code

#include <iostream>
using namespace std;
int main()
{
int age=0, Male=0, Female=0, sum =0, sum1=0,sum2=0,sum3=0,sum4=0, sum5=0, total=0, ageCount=0;
int average, totalSum, totalCount;
char gender;
int genderMale=0, genderFemale=0, lowAge=0, highAge=0;

//cout << "Enter age of attendee (-1 to quit): ";
do
{ cout << "Enter age of attendee (-1 to quit): ";
cin >> age;
cout << "Enter gender (M or F) : ";
cin >> gender;

if (gender=='M')
genderMale++;
else if (gender=='F')
genderFemale++;

if (age>=60)
sum5++;
else if (age>=41 && age<=60)
sum4++;
else if (age>=31 && age <=40)
sum3++;
else if (age>=19 && age<=30)
sum2++;
else if (age>=0 && age<=18)
sum1++;

sum+=age;
ageCount++;

}while(age!=-1);
totalSum=sum+1;
totalCount=ageCount-1;
average=totalSum/totalCount;

cout << "age 0 to 18: " <<sum1 <<endl;
cout << "age 19 to 30: " <<sum2 <<endl;
cout << "age 31 to 40: " <<sum3 <<endl;
cout << "age 41 to 60: " <<sum4 <<endl;
cout << "over 60: " << sum5 <<endl <<endl;

cout << "The average age was : " <<average<<endl;
cout << "The youngest person in attendance was " <<lowAge <<endl;
cout << "The oldest person in attendance was " <<endl;

cout << "males: " << genderMale <<endl;
cout << "females: " <<genderFemale <<endl;




return 0;
}
Last edited on
what do you need help with?
just FYI, you aren't using the variables Female, Male, Total or Highage. It'll give you a few more marks if you either use them somewhere(make sure it's useful) or just completely get rid of them.
First of all this program is supposed to quit right after when the user enters -1, but when i enter -1, it will still print the screen and asks to enter M or F. Also, how do i get the low age and upper age? Thanks.
personally, i'd have it count everytime someone entered a person's name and age, set a loop that repeats that many times that uses < or > math arithmitics to find the lowest/highest age.

it's running this block of code
1
2
3
4
if (gender=='M')
genderMale++;
else if (gender=='F')
genderFemale++;


and then

1
2
3
4
}while(age!=-1);
totalSum=sum+1;
totalCount=ageCount-1;
average=totalSum/totalCount;


it will run the -1 integer through the first block of code. and then it will reach the while and then continue. you need to include the second block of code before it asks the age. If you need any help or need me to show an example, just reply back
Last edited on
#include <iostream>
using namespace std;
int main()
{
int age=0, Male=0, Female=0, sum =0, sum1=0,sum2=0,sum3=0,sum4=0, sum5=0, total=0, ageCount=0, lowAge=0, highAge=0, average, totalSum, totalCount;
char gender;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;
if (age==-1)
{ cout << "Error. \n";
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;}
else
{while (age!=-1)
{cout << "Enter gender (M or F) : ";
cin >> gender;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;

if (gender=='M')
Male++;
else if (gender=='F')
Female++;

if (age>=60)
sum5++;
else if (age>=41 && age<=60)
sum4++;
else if (age>=31 && age <=40)
sum3++;
else if (age>=19 && age<=30)
sum2++;
else if (age>=0 && age<=18)
sum1++;
sum+=age;
ageCount++;
}}
totalSum=sum+1;
totalCount=ageCount-1;
average=totalSum/totalCount;

cout << "age 0 to 18: " <<sum1 <<endl;
cout << "age 19 to 30: " <<sum2 <<endl;
cout << "age 31 to 40: " <<sum3 <<endl;
cout << "age 41 to 60: " <<sum4 <<endl;
cout << "over 60: " << sum5 <<endl;

cout << "The average age was : " <<average<<endl;
cout << "The youngest person in attendance was " <<endl;
cout << "The oldest person in attendance was " <<endl;

cout << "males: " << Male <<endl;
cout << "females: " <<Female <<endl;

return 0;}
This is what i have ended up with yet. Most of the things are working fine. I had to add "IF" because, on the question, if user entered -1, in the beginning, I had to give an error message. So, what I am concerned is, the first age the user enters, dosent count. How do i fix that? Thank You Very much.
Found the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
char gender;
    cout << "Enter age of attendee (-1 to quit): ";
    cin >> age;
    if (age==-1)
    { cout << "Error. \n";
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;}
    else
    {while (age!=-1)
    {cout << "Enter gender (M or F) : ";
        cin >> gender;
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;


You are redefining the variable "age" on line 3 and line 13. so the first time they enter the age. it get's replaced by the variable in line 13.

I think i described the problem horribly. if so, message back saying.
Enter age of attendee (-1 to quit): 34
Enter gender (M or F) : M
Enter age of attendee (-1 to quit): 16
Enter gender (M or F) : M
Enter age of attendee (-1 to quit): 68
Enter gender (M or F) : F
Enter age of attendee (-1 to quit): 53
Enter gender (M or F) : F
Enter age of attendee (-1 to quit): 39
Enter gender (M or F) : F
Enter age of attendee (-1 to quit): 23
Enter gender (M or F) : F
Enter age of attendee (-1 to quit): 21
Enter gender (M or F) : F
Enter age of attendee (-1 to quit): -1


age 0 to 18: 1
age 19 to 30: 2
age 31 to 40: 1
age 41 to 60: 1
over 60: 1
The average age was : 36
The youngest person in attendance was
The oldest person in attendance was
males: 2
females: 5

Process returned 0 (0x0) execution time : 20.385 s
Press any key to continue.


This is my output right now with the code I have. And the code I tweaked is below:

#include <iostream>
using namespace std;
int main()
{
int age=0, Male=0, Female=0, sum =0, sum1=0,sum2=0,sum3=0,sum4=0, sum5=0, total=0, ageCount=0, lowAge=0, highAge=0, average, totalSum, totalCount;
char gender;

cout << "Enter age of attendee (-1 to quit): ";
cin >> age;

if (age==-1)
{ cout << "Error. \n";
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;
}

while (age!=-1)

//cout << "Enter age of attendee (-1 to quit): ";
// cin >> age;
{

cout << "Enter gender (M or F) : ";
cin >> gender;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;



if (age>=60)
sum5++;
else if (age>=41 && age<=60)
sum4++;
else if (age>=31 && age <=40)
sum3++;
else if (age>=19 && age<=30)
sum2++;
else if (age>=0 && age<=18)
sum1++;

if (gender=='M')
Male++;
else if (gender=='F')
Female++;
sum+=age;
ageCount++;}




totalSum=sum+1;
totalCount=ageCount-1;
average=totalSum/totalCount;

cout << "\n\nage 0 to 18: " <<sum1 <<endl;
cout << "age 19 to 30: " <<sum2 <<endl;
cout << "age 31 to 40: " <<sum3 <<endl;
cout << "age 41 to 60: " <<sum4 <<endl;
cout << "over 60: " << sum5 <<endl;

cout << "The average age was : " <<average<<endl;
cout << "The youngest person in attendance was " <<endl;
cout << "The oldest person in attendance was " <<endl;

cout << "males: " << Male <<endl;
cout << "females: " <<Female <<endl;


return 0;}

What the code was supposed to show is below. This is the output I need to get.

Enter age of attendee (-1 to quit): 34
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 16
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 68
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 53
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 39
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 23
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 21
Enter gender (M or F): F
Enter age of attendee (-1 to quit): -1

age 0 to 18: 1
age 19 to 30: 2
age 31 to 40: 2
age 41 to 60: 1
over 60: 1

males: 2
females: 5
The average age was 36.
The youngest person in attendance was 16.
The oldest person in attendance was 68.

As you can see, I am kinda closer than I was earlier. The main problem here is, the first age doesnot counts. For age 31 to 40, I am getting one less count.
I dont know if you understood it or not. If not, then please let me know.
Thank You.

I kinda understood what you are saying, but I need an input from user to determine the number of age, so line 3 is very much needed.
And again, once the program determines the number, it asks for gender, so and then the program needs to ask the age again, so don't I need that line 13 to get another age?
The line 3 is out of while condition, so it wont ask to enter the age 2nd time to user, unless i put that line 13. I dont know how much you are understanding this. Thanks.
Topic archived. No new replies allowed.