Function problem

here's the whole program

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
#include <iostream>
#include <math.h>
using namespace std;

double hatSize(int weight,int height);
double waistSize(int age,int weight, int height);


void main()
{	
	char choice ='y';
	double age;
	double height;
	double weight;

	while(choice == 'y') {
		cout << "Enter your age: ";
		cin >> age;
		if ( age <= 0 ){
			cout <<"That age is invalid" << endl;
		}
		cout << "Enter your height (in inches): ";
		cin >> height;
		if ( height <= 0 ) {
			cout <<"That height is invalid" << endl;
		}
		cout << "Enter your weight (in pounds): ";
		cin>> weight;
		if ( weight <= 0 ){
			cout <<"That weight is invalid" << endl;
		}

		cout << "Hat size: " << hatSize(weight, height) << endl;
		cout << "Waist size: " << waistSize(age, weight, height) << endl;

		cout << "Do you want to do another calculation? \nPress y for yes, n for no: ";
		cin >> choice;
		if (choice !='y' || choice !='n')
		{
			cout << choice << " is not a valid input " << endl;
			cout << "Do you want to do another calculation? \nPress y for yes, n for no: ";
			cin >> choice;
		}


	system ("pause");
	}
}

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

double waistSize (int age, int weight, int height) {
	double newWaist = weight/5.7;
	if ( age >= 28 ){
		age +=2;
		newWaist = ( newWaist + 0.1 );
	}
	return newWaist;

}


Im having problem with this part of the function, where i want it to add 0.1 inches each 2 years over age 28. please help me.

1
2
3
4
5
6
7
8
9
double waistSize (int age, int weight, int height) {
	double newWaist = weight/5.7;
	if ( age >= 28 ){
		age +=2;
		newWaist = ( newWaist + 0.1 );
	}
	return newWaist;

}
closed account (48T7M4Gy)
Each 2 years for age over 28. Hmmm

Number of 2 year periods over age 28 = (age -28) modulo 2
If you add 0.1 inches for every 2 year period then just multiply the number of 2 year periods by 0.1
Thank you figured out and fixed it to this
1
2
3
4
5
6
7
8
double waistSize(double age, double weight, double height){

	double waist = (weight/5.7);
	if(age-28 > 0){ 
		waist += (.1*((age-28)/2));
	}
	return waist; 
}
closed account (48T7M4Gy)
Too easy :-)

The modulo is a bit tougher because it means you can only earn the extra waistline addition in two year lots. Yours is more generous but that is not a computing problem.
Topic archived. No new replies allowed.