Incorrect Output

Hi, I have written this code to calculate theoretical clothing sizes according to the question in my book. However, two of my clothing size outputs are obviously incorrect (jacket size and waist size). If anyone could run this program and tell me what sort of logical errors I have made, I would greatly appreciate it.

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
double hat(double weight, double height);
double jacket(double weight, double height, int age, int x);
double waist(double weight, int age, int y);
using namespace std;
int main()
{
    cout << 1 / 8;
    int age, x, y;
    double height, weight;
    if ((age - 30) / 10 > 0)
    {
        x = (age - 30) / 10;
    }
    else if (age <= 30)
    {
        x = 0;
    }
    if ((age - 28) / 2 > 0)
    {
        y = (age - 28) / 2;
    }
    else if (age <= 28)
    {
        y = 0;
    }
    cout << "Input your age." << endl;
    cin >> age;
    cout << "Input your height in feet." << endl;
    cin >> height;
    cout << "Input your weight in pounds." << endl;
    cin >> weight;
    cout << "Your hat size is " <<  hat(weight, height) << "." << endl;
    cout << "Your jacket size is " << jacket(weight, height, age, x) << "." << endl;
    cout << "Your waist size in inches is " << waist(weight, age, y);
    return 0;
}

double hat(double weight, double height)
{
    return (weight / height) * 2.9;
}

double jacket(double weight, double height, int age, int x)
{
    return ((height * weight) / 288) + ((1.0/8.0) * x);
}

double waist(double weight, int age, int y)
{
    return ((weight / 5.7) + ((0.1) * y));
}
Here are my observations:
1. age is not initialized when you calculate x and y. Your compiler might set age=0 by default, so x=0, y=0. You need to move calculations for x and y after you input age
2. are you sure your conditions on line 10 and 18 are correct? Keep in mind that you are working with integers. So if I input age 32, on line 10 32-30 is 2, 2/10 is 0 (integers), so it is not greater than 0, also it is not less than 30, so it is undefined. I think what you want to do is
1
2
3
4
5
6
7
8
if (age>30)
{ 
   x=(age-30)*0.1;
}
else
{
   x=0;
}

Obviously you can replace *0.1 with /10 if that is the correct way
((age - 30) / 10 > 0)
and
((age - 28) / 2 > 0)

Redundant.
If the age is < 30 or 28, dividing it will always give you a number less than 0.

You can do:
1
2
3
4
((age>30)
{
    x = (age - 30) / 10;
}


Can you tell us an example input/output so that we know what result you are trying to get?
Last edited on
Topic archived. No new replies allowed.