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
134
135
136
#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 
// 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.					
// 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)
{
	double jacket_calc;
	jacket_calc = (((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)
{
	double waist_calc;
	waist_calc = ((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.

Last edited on
namespaces are just a way of making sure functions/classes, etc, don't conflict, of course doing

 
using namespace std;


completely destroys that advantage, making the namespace useless, you can get rid of that line of code and replace every

cout
cin
endl

with

std::cout
std::cin
std::endl

plus, once you declare you are using a namespace in the global part of your file (i.e, not in a function or class) you don't need to declare it everywhere else, like you did in get_input and description.

the sooner you stop using namespace std, the better. like a drug, it is hard to stop using it.
Last edited on
I just put using namespace in the global space because the is where the professor wants it to be. I think for this course I'm just going to leave it there instead of putting in my function definitions.

What is the disadvantage of just using a namespace directive inside of a function you create? I guess another way of asking that is what is the benefit of using the scope resolution operator?

Also I'd appreciate if anybody could point me in the right direction for how to create the loop in my functions that factor in age.
If you get rid of the using namespace std; in the global space, and replace all cout with std::cout, etc, your professor may end up being impressed, since you're showing you are willing to learn c++ in your own time.

the point I'm making is if it is in the global space, there is no reason to put it in your functions.

like I said, namespaces are to avoid conflicts, imagine if you had a function called cout() and you were using namespace std, the compiler wouldn't know which one to use (actually I'm not sure, it may prefer using std::cout or your function, but it should output an error)

IIRC, the scope resolution operator is ::, and basically means if you declare a variable in global space, and then declare a variable with the same name in the local scope (i.e function) the compiler will use the one in the local scope (again, not sure, it may give you an error, I haven't bothered to test it) so if you instead wanted to use the one you declared in the global space, you would type ::variablenameblahetc2134_luuuu, for example.
I put the using namespace in global and in my functions because I showed my professor my code today to ask for some help. I wanted to ask if I should just use the namespace in a function body, but I forgot because other stuff came up.

We just went over functions in class. I was reading the book because I learn better when I read and she asked why I was using preconditions. She said we didn't go over that in class. I don't think she likes people doing things a different way. We never talked about namespaces. She said just put using namespace std; after your including directives. This class is an intro c++ class for engineers. i'm not totally sure what the purpose of the class is. It seems like we aren't fully learning the language and I think this is the only programming class the vast majority of people in the class will take. I could probably write a book on how education system is broken.

Last edited on
I want to this function to do this calculation:

1
2
3
4
5
double jacket(double input_height, double input_weight, int input_age)
{
	(((input_height*input_weight)/288) + jacket_age(input_age))
	return ( (((input_height*input_weight)/288) + jacket_age) )
}


My idea is to do a calculation that is independent of age [(((input_height*input_weight)/288)] and then add to that a value that is contingent contingent on age. I want that age factor to be a different function that is called in the jacket function. Is this a bad approach? Should calculating the age factor not be done in another function?

Is it bad to use a break statement with a for loop?
Last edited on
Yeah, some teachers are like that, I remember in my high school ICT class the teacher complained simply because I clicked on show HTML (we were working in microsoft front page) anyway..

why are you doing it like that? you don't need two lines doing the same calculation

1
2
3
4
double jacket(double input_height, double input_weight, int input_age)
{
	return ( (input_height * input_weight) / 288) + jacket_age(input_age)) );
}


it doesn't really matter where you calculate the age factor as long as you do it properly.

your hat function looks like it would throw an error, same with your jacket_age function, try compiling it and see if it will actually run, even if it doesn't give you the output you want.

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
#include <iostream>
using namespace std;
 
double jacket(double input_height, double input_weight, int input_age);
 
double jacket_age(int age);
 
int main ()
{
        double input_height=60, input_weight=100, input_age=67, jacket_size;
 
        jacket_size = jacket(input_height, input_weight, input_age);
        cout << jacket_size << endl;
        return 0;
}
 
double jacket(double input_height, double input_weight, int input_age)
{
        double temp;
        temp = (((input_height*input_weight)/288));
        if (input_age >= 40)
        {
                jacket_age(input_age);
        }
        return ( temp + jacket_age(input_age) );
}
 
double jacket_age(int input_age) 
{
        int i, count;
        double temp;
        for (i=input_age, count=0; i >= 40; i=i-10)
        {
        ++count;
        temp = .125*count;
        }
        return (temp);
}


When I run the program the numerical value that is output is 20.208. When I use a calculator I get 21.083. I'm not sure why the program is off.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
double jacket_age(int input_age) 
{
        int i, count;
        double temp;
        for (i=input_age-10, count=0; i >= 40; i=i-10)
        {
        ++count;
	temp = .125*count;
        }
        return (temp);
}


I get the correct output now, but the code doesn't seem to look that great. Is initializing i to input_age-10 a bad way to initialize?
no, why would it be, you will undoubtedly have to define variables in a similar way at some point in the future.

that for loop is strange, 3 semi colons, there should only be two, your compiler might be ignoring i=input_age-10, you don't need to do it in the for loop anyway, since it won't change the value of input_age or 10.

1
2
3
4
5
6
7
8
9
10
11
double jacket_age(int input_age) 
{
        int i = input_age - 10
        double temp;
        for (int count = 0; i >= 40; i -= 10)
        {
        ++count;
	temp = .125*count;
        }
        return (temp);
}
So I don't have to declare and initialize i as an argument in the for loop expression? I guess for someone reason I thought the loop variable has to be declared and initialized in the loop.

1
2
3
4
5
6
7
8
9
10
11
double jacket_age(int input_age) 
{
        int i = input_age;
        double temp;
        for (int count = 0; i >= 40; i -= 10)
        {
        ++count;
	temp = .125*count;
        }
        return (temp);
}


I just changed the code to what is above because when I did input_age - 10 it missed when calculation. I think I had to add that -10 in my original loop, because like you said, the compiler might have ignored it in the loop.

Should I switch this:

1
2
3
4
5
6
7
8
9
10
11
double waist_age(int input_age)
{
    int i, count;
    double temp(0);
    for (i=input_age, count=0; i >= 30; i=i-2)
    {
	    ++count;
		temp = .1*count;
    }
    return (temp);
}


to this:

1
2
3
4
5
6
7
8
9
10
11
double waist_age(int input_age)
{
    int i=input_age;
    double temp(0);
    for (int count = 0; i >= 30; i -= 2)
    {
	    ++count;
		temp = .1*count;
    }
    return (temp);
}


I tried that in Visual Studio and got when I went to build and compile it was successful but when I tried to start without debugging it said there were runtime errors. Do you have any idea why that happened?
runtime errors are either due to undefined behaviour, or infinite loops, such as divide by zero, although it looks fine to me, I am a little unsure of double temp(0); seems like you are prototyping a function inside a function, but you say your compiler doesn't give you any errors? try replacing it with double temp = 0; and see if that fixes it, other than that I don't see anything particularly wrong.
It was something very stupid. I was doing something else and forgot I already ran the program and the Visual Studio dialog box was open but minimized.

temp(0) and temp = 0 are the same though right? Different syntax for initializing a variable?

My problem was having a function that returned a value and in that returned value having a function that depended on certain input. That is why I used temp for the calculations so I could have a place to store the value. I'm not sure how great the code looks, but it got the job done.

It's is probably a bad way to program, but could I just put a semicolon as the first argument in the for loop? I guess I'm trying to figure out how rigid the language is. For example, I thought since I used i for the other argument variables in the loop then I would also have to use i in the loop initializing argument? Also could count have been declared outside of the loop?
I'm not entirely sure, which is why I said I was a little unsure, the only time I've seen variables defined like that is in a class, before you even define the class body.

you can have entirely empty for loops (as long as you keep the semi colons), so yes. the loop doesn't care where you initialize the variable as long as it is within its range of scope (global space, its function space, in a loop space which it itself is in, etc)

yes count can be declared outside of the loop.
Thanks for all your help. I guess they don't go by a rep system or anything here. Its awesome that there are people that just try to help other people. Once again, thanks for answering my questions.
no problem, helping others is also a great way of learning, wasn't entirely sure about the temp(0) thing, but I've just tested it and it works fine.
Topic archived. No new replies allowed.