validation 'if' to 'for' or other suggestions

The program runs, no errors but was just wondering if it would better to use a 'for loop' instead of 'if else' statements for the function ValidateTempWS(). I do need to use an 'if' state in main that only allows valid int's selected to continue other wise asks them for new input, but with the output I'm getting from how the function is currently setup I'm not able to or can't think of a good way to do so. So really my question if a 'for loop' would be better to use for that function what's a good example?

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
137
138
139
  /*
*/


#include <iostream>
#include <math.h>// allows math fuctions such as pow() to be used
#include <string>
using namespace std;

//prototypes
double AskForTempurature();
double AskForWindSpeed();
int ValidateTempWS(double temp, double windsp);
double CalcWindchill(double temp, double windsp);
int DetermineFrostbiteTimes(double temp, double windsp);

//main fuction
int main()

{


	string again; // do loop variable
	do // start of loop, runs again if user chooses to
	{
		double currentTemp;
		currentTemp = AskForTempurature();// calls function AskForTemp... and assigns user input into currentTemp

		double windGust;
		windGust = AskForWindSpeed();// calls function and assigns user input into the variable

		int validOrNot = ValidateTempWS(currentTemp, windGust); // runs fuction and determines if values entered are valid (within range)
		
		
		cout << "When the outside temperature is " << currentTemp;
		cout << " degrees F and the wind is blowing " << windGust;
		cout << " MPH it feels like ";
		double windChill = CalcWindchill(currentTemp, windGust);
		double frostbite = DetermineFrostbiteTimes(currentTemp, windGust);
		cout << " and you have about " << frostbite;
		cout << " minutes until you get frostbite ";



		cout << "\nWould you like to do another calculation? (Yes/No)";
		getline(cin, again);
		if (again == "No") //ends program.
		{
			cout << "Good Bye. " << endl;
		}

	} while (again == "Yes");// starts program at the beginning asking for input.

	return 0; //return okay
}

//FUNCTIONS
double AskForTempurature() //simple function asking for user to input temp 
{
	double temperature;
	cout << "What is the current Tempurature? \n";
	cin >> temperature;
	cin.ignore();

	return temperature;//returns okay
}

double AskForWindSpeed()
{
	double windSpeed;
	cout << "What is the current speed the wind is blowing at? (in MPH, only number required) \n"; // simple function asking for speed of wind
	cin >> windSpeed;
	cin.ignore();

	return windSpeed;//returns okay
}

int ValidateTempWS(double temp, double windsp)// validates that temp and wind speed are within range
{
	int valid = 0; // variable assigned for return, returns one of the below 'if' or 'else if' statements. // may need to change to 'for loop'

	if (temp < 41 && temp > -46)
	{
		if (windsp < 61 && windsp > 4)
		{

			cout << "0: ALL OK! \n";
		}
	}
	else if (temp >= 41 || temp <= -46)
	{
		if (windsp >= 61 || windsp <= 4)
		{
			cout << "2: BOTH INVALID! \n";
		}
	}
	else if (temp >= 41 || temp <= -46)
	{
		cout << "3: Temp invalid \n";
	}

	else if (windsp >= 61 || windsp <= 4)
	{
		cout << "4: Wind speed invalid \n";
	} 

	
	return valid;// return okay, does what it's suppose to, may need to change to 'for loop'...
}

double CalcWindchill(double temp, double windsp) // calculates what the temp feels like depending on thermomitor temp and wind speed
{
	// T = temp, V = windsp
	double first;
	first = 35.74 + 0.6215 * temp;
	double second;
	second = 35.75 * pow(windsp, 0.16);
	double third;
	third = 0.4275 * temp * pow(windsp, 0.16);
	double solution;
	solution = first - second + third; //35.74 + 0.6215*T -35.75*(V ^ 0.16) + 0.4275*T*(V ^ 0.16);
	cout << solution;
	return solution;// formula correct. returns okay
}

int DetermineFrostbiteTimes(double temp, double windsp) // calculates time until receiving frostbite.
{
	// T = temp, V = windsp
	double first; 
	first = -24.5 * (0.667 * (windsp * 8/5 + 4.8)) + 2111;
	double second;
	second = -4.8 - (temp - 32) * 5 / 9;
	double third;
	third = pow(second, -1.668);
	double solution;	
	solution = first * third; //((-24.5 * ((0.667 * (V * 8/5)) + 4.8)) + 2111) * (-4.8 - ((T - 32) * 5/9))^ -1.668
	
	return solution; // formula correct. returns okay
}
closed account (48T7M4Gy)
Opinions vary but choose the one that is the most readable and therefore checkable and maintainable.

FWIW if else is my preference and I try to avoid else if. If it gets too complicated it can, but not always, indicates something is going wrong.

1
2
3
4
5
6
7
8
9
10
11
if (windsp < 61 && windsp > 4)
    cout << "0: ALL OK! \n";
    
if (windsp >= 61 || windsp <= 4)
	cout << "2: BOTH INVALID! \n";
				
if (temp >= 41 || temp <= -46)
	    cout << "3: Temp invalid \n";

if (windsp >= 61 || windsp <= 4)
    cout << "4: Wind speed invalid \n";
First, one should note that your function, ValidateTempWS, is not correct. You only think it is because you are ignoring the return value (which is always 0 under any circumstance.)

Second, note that kemort's code is just.. well, wrong, logically speaking.

Here's another way you could write the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int ValidateTempWS(double temp, double windsp)// validates that temp and wind speed are within range
{
    const double min_temp = -45, max_temp = 40;
    const double min_windspeed = 5, max_windspeed = 60;

    bool temp_is_valid = temp <= max_temp && temp >= min_temp;
    bool windspeed_is_valid = windsp <= max_windspeed && windsp >= min_windspeed;

    if (!temp_is_valid && !windspeed_is_valid)
        std::cout << "Both invalid!\n";
    else if (!temp_is_valid)
        std::cout << "Temperature is invalid!\n";
    else if (!windspeed_is_valid)
        std::cout << "Wind speed is invalid!\n";
    else
        std::cout << "All OK!\n";

    return temp_is_valid && windspeed_is_valid;
}

Last edited on
closed account (48T7M4Gy)
Second, note that kemort's code is just.. well, wrong, logically speaking.


Please address the OP and not make unhelpful critiques of other people's good faith contributions or as in this case a suggested way of looking at the choices which is the subject of this thread.

Uninformed and ego-driven comments are counterproductive and fail to meet the spirit of help.

It is up to the OP whether they take any or all comments.

Your arrogance and inexperience never fail to show themselves cire.
Please address the OP

I was addressing the OP. You, on the other hand, can't seem to follow your own advice.
closed account (48T7M4Gy)
I was addressing the OP.

No you weren't. You didn't like being exposed on the other thread for the C++ wanker you are. You are a loser as well as a wanker because you can't contribute positively due to your dismal ego-driven rubbish you keep coming up with post after post.
Thank you for both your replies. Cire your method corrected me getting negative times. I didn't mention in OP but I need to return an int and then in main if the values entered are correct it proceeds through otherwise asks if they want to try again. I know how I would use an if statement in main but since each block of code doesn't look into one another and I can only call a function what would be a good method? example:
1
2
3
4
5
6
7
8
9
10
11
if (validOrNot == 0)
{
cout << "When the outside temperature is " << currentTemp;
			cout << " degrees F and the wind is blowing " << windGust;
			cout << " MPH it feels like ";
			double windChill = CalcWindchill(currentTemp, windGust);
			double frostbite = DetermineFrostbiteTimes(currentTemp, windGust);
			cout << " and you have about " << frostbite;
			cout << " minutes until you get frostbite ";
}
else 


I run this and since the ValidateTempWS() isn't returning an int such as 0,1,2,3 it just skips the if statement and asks to do another.
Last edited on
If 0 is supposed to indicate success, the return value in the code I supplied will need adjustment.

Otherwise, it looks good to me. Having issues?

(Oh, and you should probably finish outputting the thought on line 5.)
Topic archived. No new replies allowed.