Need direction with code issue

Hey everyone just wondering if someone can help me.
I'm working on a program that calculates a men's suit size based on weight and height. Age is also a variable. I have all of it good to go except I have on issue I am actually totally unsure of where to even begin writing this piece of code.
What I need is for weight to be divided by 5.7 but then for every other year that the user is over the age of 28 (30, 32, 34, etc), I want to add .1.

Could anybody give me any type of ideas? I am not sure if a loop of some kind will get the job done or if I just have to write a ton of IF statements. Thanks.
It can be done without any loops or ifs. It's very easy, if you think about it for a moment.
@helios: I'd be very thankful if you could review my code (data-types, formula) or post your own take.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
	int age;
	float height;
	float weight;
	cout << "Age: ";
	cin >> age;
	cout << "Height: ";
	cin >> height;
	cout << "Weight: ";
	cin >> weight;
	cout << weight/(5.7 + (age>28?((age-28)/2):0)*0.1);
	// @ccdwiu: you didn't say what to do with height
	return 0;
}
Last edited on
@Fresh Grass

Ok I got it with help from you, fresh grass, thanks. Here is what I got:
 
cout << weight/5.7 + (age>28?(age-28)/2:0*.1); 

You came up with this formula I just rearranged a bracket

I came accross this small program as I was browsing on yahoo questions, I have been learning c++ for like a week so I decided to give it a try, here are the questions:

Write a program that asks for the user's height,weight and age and then computes clothing sizes according to the formulas.
~ Hat Size = Weight in pounds divided by height in inches and all that multiplied by 2.9

~ Jacket Size ( chest in inches)= height times weight divied by 288 and then adjusting by adding 1/8 of an inch for each 10 years over age 30.( Note thath the adjustment only takes place after a full 10 years. So there is no adjustmen for ages 30 through 39, but 1/8 of an inch is added for age 40)

~ 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.So there is no adjustment for age 29, but 1/10 of an inch added for age 30)

Use functions for each calculation.Your program should allow the user to repeat this calculation as often as the user wishes.

And here is my final 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

double hat (double w, double h);
double jacket (double w, double h, int a);
double waist (double w, double h, int a);
int main ()

{
double height;
double weight;
int age;
char yesno;
do{
system("CLS");
cout << "Please enter your height (in inches):" << endl;
cin >> height;
cout << "Please enter your weight (in pounds):" << endl;
cin >> weight;
cout << "Please enter your age (in years):" << endl;
cin >> age;
cout << "Your hat size is: " << hat(weight, height) << endl;
cout << "Your jacket size is: " << jacket(weight, height, age) << endl;
cout << "Your waist size is: " << waist(weight, height, age) << endl;

cout << "Do you want to run again?" << endl;
cin >> yesno;
}
while (yesno == 'y'|| yesno == 'Y');
system("PAUSE");
return 0;
} 

double hat (double w, double h)
{
double r;
r = (w / h)*2.9;
return (r);
}

double jacket (double w, double h, int a)
{
double r;
r = (w*h) / 288;
if (a >= 30 && a < 40)
	r = r + .125;
else if (a >= 40 && a < 50)
	r = r + .25;
else if (a >= 50 && a < 60)
	r = r + .375;
else if (a >= 60 && a < 70)
	r = r + .5;
else if (a >= 70 && a < 80)
	r = r + .625;
else if (a >= 80 && a < 90)
	r = r + .75;
else if (a >= 90 && a < 100)
	r = r + .875;
return (r);
}

double waist (double w, double h, int a)
{
double r;
r = w/5.7 + (a>28?(a-28)/2:0*.1); 
return (r);
}





I understand there were probably much easier ways to do this but please keep in mind I just started coding about a week ago...
Ok I got it with help from you, fresh grass, thanks. Here is what I got:
cout << weight/5.7 + (age>28?(age-28)/2:0*.1);
You came up with this formula I just rearranged a bracket


I don't think that modified formula works okay:
weight/5.7 + ( if (age>28) then ((age-28)/2) else (0*0.1) )

It should be:
weight/5.7 + ( if (age>28) then ((age-28)/2) else (0) ) * 0.1

Here is my version:
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
#include <iostream>
using namespace std;

float hat(float weight, float height) {
	return weight/height*2.9;
}
float jacket(float weight, float height, int age) {
	return weight*height/288 + (age>=30?age/10-2:0)*0.125;
}
float waist(float weight, int age) {
	return weight/(5.7 + (age-28>0?(age-28)/2:0)*0.1);
}

int main() {
	for(;;) {
	// Get data //
		int age;
		float height;
		float weight;
		cout << "Age: ";
		cin >> age;
		cout << "Height: ";
		cin >> height;
		cout << "Weight: ";
		cin >> weight;
	// Output data //
		cout << "Hat size: " << hat(weight, height) << endl;
		cout << "Jacket size: " << jacket(weight, height, age) << endl;
		cout << "Waist size: " << waist(weight, age) << endl;
	// Loop question //
		int loop;
		cout << "Want to run again? (1/0) ";
		cin >> loop;
		if (!loop) return 0;
		cout << endl;
	}
}

Right on, thanks
Topic archived. No new replies allowed.