Intro to Computer Science project here. Calculates the hat, waist, and jacket size of the user when given age, height, and weight. Seems to output correctly.

Here are the guidelines for the assignment.

Write a program that asks for the user’s height, weight, and age, and then computes clothing sizes according to the formulas:

  1. Hat size = weight in pounds divided by height in inches and all multiplied by 2.9

  2. Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over the age of 30. (Note that the adjustment only takes place after a full 10 years. Thus, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)

  3. Waist in inches=weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. Thus, there is no adjustment for age 29, but 1/10 of an inch is added for age 30.)

Use functions for each calculation. (Thus, write 3 functions.)

I did this a tad hastily so pardon any messiness.

I am required to use namespace stdas it makes it easier for the teacher to read.

#include <iostream>

using namespace std;

double hat(double,double);

double jacket(double,double,int);

double waist(double,double,int);

int main ()
    double height, weight;
    int age;
    char answer;


        cout<< "Enter the user's height in inches: ";
        cout<< "Enter the user's' weight in pounds: ";
        cout<< "Enter the user's' age: ";
        cout<< "\tThe user's Hat size: " << hat(weight ,height) << "\tThe user's Jacket size: "<< jacket( height, weight, age) << "\tThe user's Waist size: "<< waist( height, weight, age)<< "\n \nWould you like to continue (y/n)? ";
        while(toupper(answer) == 'Y');
        return 0;

    double hat(double weight ,double height)

        return ((weight/height) * 2.9);

    double jacket(double height,double weight,int age)
        double size;
        int j;
        if (age>=30)
        if((age % 10) !=0)
        age = age-(age%10);
        j= (age-30)/10;
        size =((height * weight) / 288)+((1.0/8)*j);
        size =((height * weight) / 288);
        return size;

    double waist(double height,double weight,int age)
        double size2;
        int k;

        if(age >= 28)
            if((age % 2) !=0)
            age = age-(age%2);
            k = (age-28)/2;
            size2 = (weight/(5.7))+( (1.0/10)*k);
        size2 = weight / (5.7);
        return size2;

1 Answer 1


I see some things that may help you improve your code.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. An alternative that may still meet your needs may be to put it within main rather in the global context.

Eliminate parentheses to enhance readability

Adding parentheses where they are not needed makes the code harder to read and comprehend than it needs to be. For example, in the hat function, we have this:

return ((weight/height) * 2.9);

With no change in the effective calculation, we can simply write this:

return weight / height * 2.9;

I think that's a little easier to read.

Fix the formatting

It seems you may already know this, but your indentation and use of whitespace is not consistent. It's less important which particular conventions you use than choosing one and applying it consistently.

Specify parameter names in function prototypes

With a function prototype like this:

double hat(double,double);

I can't tell which parameter is the height and which is the weight without looking at the implementation code. Since function prototypes tend to become the documentation for your code's public interface, make it easier on the user of these functions by writing the prototype like this:

double hat(double weight, double height);

Or even like this to make the units unambiguous:

double hat(double weightInPounds, double heightInInches);

Don't pass parameters that are not needed

The waist routine does not need or use the height, so that parameter can and should be eliminated.

Use all required #includes

The code uses toupper() but doesn't include the corresponding header. The code should have

#include <cctype>

Simplify expressions

The jacket and waist calculations are more complex than they need to be. For example, for the waist routine, one could write this:

double waist(double weight, int age)
    double waist = weight / 5.7;
    if (age > 28) {
        waist += (age - 28) / 2 * 0.1;
    return waist;

Note that the calculation (age - 28) / 2 is done using integer math, so that subexpression will yield 0 for ages of 28 or 29 as per the specification. Understanding implicit conversions and particularly numeric conversions will be very helpful to you as you write future programs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.