Doing calculations with functions

This is my assignment:

Problem - 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 divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30. (Note that the adjustment only takes place after a full 10 years. So, there is no adjustment 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 is 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.

Also make sure to robustly confirm that the height, weight, and age are positive non-zero values.

Note: “robust” means that your program should not continue on until a valid input is provided. Which structure would allow for this?

Here is my 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
using namespace std;

void description();
// Postcondition: Description of the program is printed to the screen.

void get_input(double& input_height, double& input_weight, int& input_age);
// Precondition: User enters correct values.
// The value of if input_height has been set to the person's height.  The value of 
// input_weight is set to the person's  weight, and the value of input_age is set 
// to the peron's age

double hat(double input_height, double input_weight);
// Precondition: Both values are positive and height is in inches 
// and weight is in pounds. 
// Calculates the hat size of the user.

double jacket(double input_height, double input_weight, int input_age);
// Precondition: All three values are positive. Height is in inches, weight is 	   //If sattement used to call jacket_age function if age is 40 or greater.
// in pounds, and age is an integer.
// Calculates the jacket size (chest measurement in inches) of the user.

double jacket_age(int age);
// Precondition: Age is a positive integer.					//Use while loops (age==40, age==50, etc)
// Starting at age 40, adds 1/8 of an inch to jacket size for every 10 years older 
// the user is. 

double waist(double input_height, double input_weight, int input_age);
// Precondition: All three values are positive. Height is in inches, weight is 
// in pounds, and age is an integer.
// Calculates the waist in inches of the user.

double waist_age(int age); 
// Precondition: Age is a positive integer.
// Starting at age 30, adds 1/10 of an inch for every 2 years older the user is.

double show_output(double hat_calc, double jacket_calc, double waist_calc);
// Precondition: hat size, jacket size, and waist size were calculated correctly.
// outputs the calculates sizes to the user.

int main () 
{
	double height, weight, hat_size, jacket_size, waist_in_inches;
	int age;
	char answer;
	
	do
	{
		description();
		get_input(height, weight, age);
		hat_size = hat(height, weight);
		jacket_size = jacket(height, weight, age);
		waist_in_inches = waist(height, weight, age);
		show_output(hat_size, jacket_size, waist_in_inches);

		cout << "Would you like to run this program again?\n";
		cin  >> answer;
	}
	while (answer == 'y' || answer == 'Y');
	
	return 0;
}


//Uses iostream
void description()
{
	using namespace std;
	cout << "This program determines hat size, jacket size, and waist in inches\n";
	cout << "based on height, weight, and age.\n";
}

//Uses iostream
void get_input(double& input_height, double& input_weight, int& input_age)
{
	using namespace std;
	cout << "Enter your height (to the nearest half inch): ";
	cin >> input_height;
	while ( input_height <= 0)
	{
	   cout << "Enter a positive number: ";
	   cin >> input_height;
	}
	cout << "Enter your weight (to the nearest half pound): ";
	cin >> input_weight;
	while ( input_weight <= 0)
	{
	   cout << "Enter a positive number: ";
	   cin >> input_weight;
	}
	cout << "Enter your age (to the neatest whole number): ";
	cin >> input_age;
	while ( input_age <= 0)
	{
	   cout << "Enter a positive number: ";
	   cin >> input_age;
	}
}

double hat(double input_height, double input_weight)
{
	((input_weight/input_height)*2.9);
	return ( (input_weight/input_height)*2.9 );
}

double jacket(double input_height, double input_weight, int input_age)
{
	(((input_height*input_weight)/288) + jacket_age)
	return ( (((input_height*input_weight)/288) + jacket_age) )
}

jacket_age(int input_age) 
{
	double age_factor(0), jacket(0);
	for (int i=30
}

double waist(double input_height, double input_weight, int input_age)
{
	((input_weight/5.7) + waist_age)
	return ( ((input_weight/5.7) + waist_age) )
}

// waist_age 
// Same structure as jacket_age function


double show_output(hat_calc, jacket_calc, waist_calc)
{
	cout << "Your hat size is " << hat_size << endl;
	cout << "your jacket size (chest in inches) is << jacket_size << endl;
	cout << "Your waist in inches is " << waist_in_inches << endl;
} 






I’m not sure how to calculate the age factor for the jacket and the waist calculations. I was told to make them calculate indefinitely. So it would work for some aged 70 or 170. The jacket and height age factor functions/calculations are basically the same functions just different arguments. I want to use a for loop and initialize the loop variable to 40 because age does not factor into the jacket calculation for ages <40. I’m not sure how to take the user’s input and then determine the value of the jacket age factor.

Is it better to put declare the namespace inside of the individual function rather than outside main? Also is what you are doing “declaring” a namespace. In my class we haven’t gone over namespaces and I don’t think we will, but I have a much better understanding when I know about everything I’m using.

Topic archived. No new replies allowed.