Functions written incorrectly, but I get desired result??

Ok there are two functions. The first function(userinput) ask the user to input two values. The second function has a formula and uses the first functions inputs to calculate and return a floating point number. Now I called the second function in main and everything works, even used a calculator to verify that the formula works.

My professor on the other hand said I wrote my functions incorrectly and I am scratching my head thinking if its written incorrectly, why does it work like I want it to? SO please are my functions written incorrectly and can it be written better? If so please post it. Thank you.

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
 

[code]
 #include <iostream>

#include <cmath>

using namespace std;

void userinput(float& x, float& y);			//function declarations
float windchillcalculate(float windchill);

int main(){
	float a=0;								

	windchillcalculate(a);						//calls the calculated function
	
	return 0;

}

void userinput(float& x, float& y){			//1st function  ask user for input

	cout << "Please enter the wind speed in miles per hour: ";
	cin >> x;
	cout << "Please enter the temperature in degrees farenheight: ";
	cin >> y;
}
float windchillcalculate(float windchill){							//second function calls 1st function an uses its values to calculate and return windchill
	float x = 0,y = 0;
	userinput(x,y);
	
	windchill = 35.74 + .6215*y - 35.75*pow(x, .16) + .4275*y*pow(x, .16);
	cout << "The wind chill factor is: " << windchill;
	return windchill;
}

[/code]
if its written incorrectly, why does it work like I want it to?
Maybe because what you want isn't the same as what your professor wants?

I'd say that function windchillcalculate() is doing too many other things apart from the calculation. Let main() handle some of the work.

Remove the call to function userinput (line 31) and remove the cout statement (line 34).

Also the input parameters to function windchillcalculate() should be the two values x and y, not a single value.
Last edited on
Why should windchill have 2 input parameter, when its job is just to return the value of a single formula?
Last edited on
Why should windchill have 2 input parameter

Because the calculation
 
35.74 + .6215*y - 35.75*pow(x, .16) + .4275*y*pow(x, .16);

requires two parameters, x and y.
Alright cool, here is updated code, what do you think? I tested it an it works.
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
#include <iostream>

#include <cmath>

using namespace std;

void userinput(float& x, float& y);			//function declarations
float windchillcalculate(float a, float b);

int main(){
	float c = 0,d=0;
	userinput(c, d);
	cout << "The wind chill factor is: " << windchillcalculate(c, d);						//calls the calculated function

	return 0;

}

void userinput(float& x, float& y){			//1st function  ask user for input

	cout << "Please enter the wind speed in miles per hour: ";
	cin >> x;
	cout << "Please enter the temperature in degrees farenheight: ";
	cin >> y;
}
float windchillcalculate(float a, float b){							//second function calls 1st function an uses its values to calculate and return windchill
	
	float windchill;

	windchill = 35.74 + .6215*b - 35.75*pow(a, .16) + .4275*b*pow(a, .16);
	
	return windchill;
}
That looks good to me. (though I haven't tested it).

Now this is not a criticism, merely an idea. You might simplify the function like this:

1
2
3
4
5
float windchillcalculate(float a, float b){							

    return 35.74 + .6215*b - 35.75*pow(a, .16) + .4275*b*pow(a, .16);
	
}
Awesome thank you for helping me and for responding so quick, this community is truly a marvel.
I'd also suggest using more meaningful variable names for that function:

1
2
3
float windchillcalculate(float windspeed, float temperature)
{
...


Then you won't need to guess which variable is which.

Topic archived. No new replies allowed.