C++ input validation

During winter when it is very cold, typically, everyone would like to know the windchill factor, especially, before going out. Meteorologists use the following formula to compute the windchill factor, W:
W = 35.74 + 0.6215 * T – 35.75 * V^0.16 + 0.4275 * T * V^0.16,
Where V is the wind speed in miles per hour and T is the temperature in degrees Fahrenheit. Write a program that prompts the user to input the wind speed, in miles per hour, and the temperature in degrees Fahrenheit. The program then outputs the windchill factor. Your program must contain at least two functions: one to get the user input and the other to determine the windchill factor.
That is my assigment one of the functions needs to be reference parameters to pass the values back to main (which I need help on) The other one is the input validation the windchill range is _45 to 45 F and the win speeds 3 to 60 mph but when I test it for values that are between that range it doesn't calculate it gives me my own message that i am out of range. What did I do wrong?
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
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;


static double gWindChill[11][12];


double calcWindChill (int degree, int mph);
double lookupWindChill (int degree, int mph);
double interpolateWindChill (int degree, int mph);

int
main (void)
{
	int degree, mph;

	
	for (int i = 0; i < 11; i++)
		for (int j = 0; j < 12; j++)
			gWindChill[i][j] = calcWindChill (-45 + i * 5, 5 + j * 5);

	while (true) 
	{
		
		static double result;

		cout << " Degree (F): "; 
		cin >> degree;
		cout << " Wind speed (MPH): ";
		cin >> mph;

	
		if ((degree < -45 || degree > 45) ||
		    (mph < 3 || mph > 60 || mph % 3 != 0)) 
		{
			cout << " At least one value is out of range!\n"
			     << " Ranges are:\n"
			     << " - Temperature (-45 - 45)\n"
			     << " - Wind speed (3 - 60) in increments of 5\n" << endl;

			
			return 1;
		}

		result = lookupWindChill (degree, mph);
		cout<<fixed << showpoint << setprecision(1) << "    Wind Chill for (" << degree << ", " << mph << "): "
		     << result << "\n\n";
	}

	return 0;
}


double
calcWindChill (int degree, int mph)
{
	
	return 	35.74 + 0.6215*degree -
		35.75*pow (mph, 0.16) + 0.4275*degree*pow (mph, 0.16);
}


double
lookupWindChill (int degree, int mph)
{
	
	degree += 10;
	mph -= 5; mph /= 5;

	if (degree % 5 == 0) {
		degree /= 5;
		return gWindChill[degree][mph];
	}
	else
		return interpolateWindChill (degree, mph);
}


double
interpolateWindChill (int degree, int mph)
{
	int mod;
	double lower, higher;

	mod = degree % 5;
	lower  = gWindChill[(degree - mod) / 5][mph];
	higher = gWindChill[(degree - mod + 5) / 5][mph];

	
	return ((5-mod)/5.0) * lower + (mod/5.0) * higher;
}
Last edited on
Please give your program output.
And your expected example output (same input) that you should have?

And, please wrap your code between two <code> tags ('Edit' then choose <>) This makes your code easier to read and more people will ready to help you.

Hope this helps :)
this is my output:
 Degree (F): 32
 Wind speed (MPH): 32
 At least one value is out of range!
 Ranges are:
 - Temperature (-45 - 45)
 - Wind speed (3 - 60) in increments of 5

Press any key to continue . . .


and these are the numbers i need to test my program with
 RUN YOUR PROGRAM with the following script:
Wind Speed               Temperature
32                     32
25                     50
                         46
                         40
61
60                     15
15                      0
Last edited on

32 32
25 50
46
40
61
60 15
15 0


You showed an array of numbers and certainly I haven't understood these numbers.
Be specific. More information? (Add comments)
Those numbers are the ones I need to test my code with the left side is the wind speed and the right the temperature. They are not in my code just to test.
Degree (F): 32
Wind speed (MPH): 32
At least one value is out of range!
...

This is right or wrong result? Correct output for this input?
Last edited on
Okay I fixed my code so it is easier to read now.
cout << " At least one value is out of range!\n"
<< " Ranges are:\n"
<< " - Temperature (-45 - 45)\n"
<< " - Wind speed (3 - 60) in increments of 5\n" << endl;

This is your right condition?
Yes, the input validation for the windchill range is -45 to 45 F and the win speeds 3 to 60 mph.
Try running your program :

cout << " Degree (F): ";
cin >> degree;
cout << " Wind speed (MPH): ";
cin >> mph;


if ((degree < -45 || degree > 45) ||
(mph < 3 || mph > 60 || mph % 3 != 0))
degree = 32;
mph = 32;
.............................................

degree < -45 (32 < -45) - Wrong !!!
degree > 45 (32 > 45) - Wrong !!!

mph < 3 (32 < 3) - Wrong !!!
mph > 60 (32 > 60) - Wrong !!!
mph % 3 != 0 (32 % 3 = 2) - Right !!!

Then output is :
[quote]
cout << " At least one value is out of range!\n"
<< " Ranges are:\n"
<< " - Temperature (-45 - 45)\n"
<< " - Wind speed (3 - 60) in increments of 5\n" << endl;


Why your code has
mph % 3 != 0
?????
Last edited on
It is working now after getting rid of the
mph % 3!=0

thank you!
Topic archived. No new replies allowed.