Need Help with my logic error for wind chill factor program.

Hello Community,
I have an issue with my source code. I believe everything is in order when I do the math on paper it comes out a different result then what I am getting as an output from my source 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
// Wind Chill Factor.cpp 
/*Write a program that inputs the current temperature, the temperature scale (c for Celsius or f for Fahrenheit), and the wind speed in miles per hour
, then calculates and outputs the wind chill factor using the same temperature scale as that which was entered as input. 
Temperature and wind speed may include decimal places. Temperature output should use one decimal place. 
You may assume that input will be valid and the scale will be entered in lowercase.
*/
//created by: J L
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
	double Fahrenheit, F, Celsius, C, Windchill, V, windspeed;
	

	cout << "Hello there...\n\nThis program is a windchill factor calculator.\n";
	while (1)
	{
		char option[2];
		cout << "\nEnter the number of one of the following:\n"
			<<"MENU:\n\n"
			<< "1.Wind Chill Factor for Fahrenheit.\n"
			<< "2.Wind Chill Factor for Celsius.\n"
			<< "3. Exit Program.\n"
			<< "Choice:";
		cin >> option;
		if (option[0] == '1')
		{
			system("CLS");
			cout << "Enter a value for Celsius to be converted to Fahrenheit: ";
			cin >> Celsius;
			cout << "\nEnter a value for wind speed: ";
			cin >> windspeed;
			C = Celsius;
			V = windspeed;
			F = (5 / 9) * C + 32;
			Windchill = 35.74 + 0.6215 * F - 35.15 * pow(F, 0.16) +0.4275 * F * pow(V,0.16);
			cout << "The temperature in Fahrenheit is " << F << " degree.\n"
				<< "The windspeed is: " << V << " miles per hour.\n"
				<< "The Wind Chill is: " << Windchill << endl;
			system("PAUSE");
			system("CLS");
		}
		else if (option[0] == '2')
		{
			system("CLS");
			cout << "Enter a value for Fahrenheit to be converted to Celsius: \n";
			cin >> Fahrenheit;
			cout << "\nEnter a value for wind speed: \n\n";
			cin >> windspeed;
			F = Fahrenheit;
			V = windspeed;
			C = 9/5 * (F - 32);
			Windchill = 35.74 + 0.6215 * C - 35.15 * pow(C, 0.16) + 0.4275 * C * pow(V, 0.16);
			cout << "The temperature in Celsius is " << C << "degree.\n"
				<< "The Wind Speed are: " << V << "mph.\n"
				<< "The Wind Chill is: " << Windchill << endl;
			system("PAUSE");
			system("CLS");
		}
		else if (option[0] == '3')
		{
			return 0;
		}
		else if (option[0] > '3' || option[0] < '1')
		{
			system("CLS");
			cout << "Invalid Entry";
			system("PAUSE");
			system("CLS");
		}
		else
		{
			system("CLS");
			cout << "Invalid Entry";
			system("PAUSE");
			system("CLS");
		}
	}
}
Last edited on
It would help if you gave an example of your input, expected (correct) output, and actual (wrong) output.

I'd bet the issue is lines 39 and 56. (5 / 9) and 9/5 both do integer division, because both operands are integers. Change to (5.0 / 9.0) and 9.0/5.0.
To perform floating-point division, at least one operand must be a floating-point number (double).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
		else if (option[0] > '3' || option[0] < '1')
		{
			system("CLS");
			cout << "Invalid Entry";
			system("PAUSE");
			system("CLS");
		}
		else
		{
			system("CLS");
			cout << "Invalid Entry";
			system("PAUSE");
			system("CLS");
		}

This is quite redundant, why not just remove lines 68 to 74 (1-7 in my excerpt)?
Last edited on
I found my error it was in the calculation of temperature.

 
			C = (F - 32) * 5/9;


but my output for a negative value doesn't make sense

Enter a value for Fahrenheit to be converted to Celsius:
-17

Enter a value for wind speed:

8
The temperature in Celsius is -27.2222degree.
The Wind Speed are: 8mph.
The Wind Chill is: -nan(ind)
Press any key to continue . . .

I don't understand why???
Well, I don't know much about wind chill, but let's take a look.

Let's suppose C = -27.2222, like you said.
Look at pow(C, 0.16)

(-27.2222)^0.16 = 1.48677017 + 0.817358815 i
(google) https://www.google.com/search?q=%28-27.2222%29%5E0.16

This is a complex number, which becomes NaN (not a number) for a double, because doubles can only handle real numbers. C++ aside, I'm not sure if your wind chill equation is correct.

Edit:
https://en.wikipedia.org/wiki/Wind_chill#North_American_and_United_Kingdom_wind_chill_index
According to Wikipedia, only velocity is being raised to a power, not the temperature.
Your equation in your code does not follow this.
Last edited on
Thanks for that it was another arithmetic issue.

Enter a value for Fahrenheit to be converted to Celsius:
32

Enter a value for wind speed:

5
The temperature in Celsius is 0degree.
The Wind Speed is: 5mph.
The Wind Chill is: 35.74
Press any key to continue . . .
Topic archived. No new replies allowed.