help with homework

closed account (jh5z8vqX)
i am struggling greatly with a programming project for class. it is only the second week of class and i still can't grasp the concepts.

the problem is:

The customer wants you to design and implement a program to calculate the average age of males and females entering a certain facility during the day. once a visitor stops at the security office, the following information is collected : 1. age 2. gender (m for male and f for female)

write and implement an algorithm to calculate and print the average age of the females and the average age of the males entering the facility. the project requirements are as follows:

1. execute the data collection until a 0 (zero) is entered as the age at which point you compute the average ages for both genders
2. males and females enter the facility in random order
3. the data collected for each visitor will consist of the following: age (a numerical value) and gender (M or F, a character).
4. validate the input given to the program:
a. no negative ages allowed
b. no age over 100 years is allowed
c. no characters aside from M or F are allowed as gender.
d. if invalid input is detected, disregard the entry (both age and gender, if entered). DO NOT TERMINATE THE PROGRAM

any help would be much much much appreciated

1: You want to execute this at least once so you'll want to use the do{...]while(CONDITION); version of the while loop.

2: You need to prompt first for their gender then their age for each entrant. You need two different variables for age, one for each gender. Rather then making an array or some thing like that just keep a running total of the age then another count of how many people were polled for each gender, this simplifies a few things in the program.

3: See 2.

4: This standard header file should help you out determine the type of data entered: http://www.cplusplus.com/reference/cctype/
Its not very good to ask for help here,better ask your teacher However here is the 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
#include<iostream>
using namespace std;
int main()
{
    long long age=0;//4 byte number
    char gender='M';//Single character
    short choise;//1 byte number
    while(true)//makes it loop forever
    {
        cout<<"Please enter the age and the gender"<<endl;
        cout<<"Age please"<<endl;
        cin>>age;
        cout<<endl<<"Gender please"<<endl;
        cin>>gender;
        if((age<20 or age>100)or (gender!='M' and gender!='F'))//checks if the age is between 20 and 100
        {
            cout<<"Hmm...You enterd some wrong data, please try again!"<<endl;
            cout<<"Age please"<<endl;
            cin>>age;
            cout<<endl<<"Gender please"<<endl;
            cin>>gender;
        }
        else
        {
            if(gender=='M')
            {
                cout<<"A "<<age<<" aged Male entered"<<endl;
                gender='M';
                age=0;
            }
            else if(gender=='F')
            {
                cout<<"A "<<age<<" aged female entered"<<endl;
                gender='M';
                age=0;
            }

        }

    }
    return 0;
}

I think everything is self explaining ,but if you have some questions ask.
Last edited on
closed account (jh5z8vqX)
holy hell thank you guys so much. however BToven when 0 is entered for the age the average age for both gender is not calculated. thanks anyways
We don't give out full samples of code, BToven was technically following the forum rules by leaving that part out.

EDIT: It's also why the while loop is broken, there is only one variable for 'age' etc. He left quite a bit of work for you to do but gave you a good head start.
Last edited on
closed account (jh5z8vqX)
ahhhhh i see. thanks a bunch guys
Topic archived. No new replies allowed.