Please check my code

The problem was that when I put a false temperature or a false wind speed. It display correctly. However, when I put both false value, it only display "Invalid wind speed." How can I make it display both else if and else condition?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main ()

{
	double temp = 0.0;
	double windspeed = 0.0;
	cout << "Enter the temperature and the wind speed: ";
	cin >> temp >> windspeed;


	if ((temp > -58) && (temp < 41) && (windspeed >= 2))
		cout << "The wind-chill temperature is " << 35.74 + (0.6215 * temp) - (35.75 * pow (windspeed, 0.16)) + (0.4275 * temp * pow (windspeed, 0.16)) << endl;
	
	else if (windspeed < 2)
		cout << "Invalid wind speed" << endl;
	else 
		cout << "Invalid temperature" << endl;

	
	
		return 0;
}
Last edited on
1
2
3
//after first if...
else if(temp < -58 || temp > 41 && windspeed < 2)
     cout << "Invalid windspeed and temperature" << endl;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{   double temp = 0.0;
	double windspeed = 0.0;
	bool temp_ok, ws_ok;
	
	cout << "Enter the temperature and the wind speed: ";
	cin >> temp >> windspeed;

    temp_ok = (temp > -58) && (temp < 41) ;
    ws_ok = (windspeed >= 2);
	if (temp_ok && ws_ok)
		cout << "The wind-chill temperature is " << 35.74 + (0.6215 * temp) - (35.75 * pow (windspeed, 0.16)) + (0.4275 * temp * pow (windspeed, 0.16)) << endl;
    if (! ws_ok)
		cout << "Invalid wind speed" << endl;
	if (! temp_ok) 
	    cout << "Invalid temperature" << endl;
	system ("pause");
	return 0;
}
Thank you. Also, why (temp < -58 || temp > 41 && windspeed < 2) can go through but not (temp < -58 && temp > 41 && windspeed < 2)?
why (temp < -58 || temp > 41 && windspeed < 2) can go through but not (temp < -58 && temp > 41 && windspeed < 2)?

Consider the order of evaluation. && has higher precedence than ||. Therefore, your statement looks like:
 
if (temp < -58 || (temp > 41 && windspeed < 2) )  // note the additional parens to show order of evaluation 


http://www.cplusplus.com/doc/tutorial/operators/
Thank you.
I mean yeah, precedence, but your temperature can't be less than 58 AND greater than 41 simultaneously, and if it is... well, you've goofed real bad.

EDIT: And yes, I did forget the parentheses (my bad), and yes, you definitely do need them around the temp checks, because in this case it produces the worst possible type of error: sometimes it correctly reports, and sometimes it doesn't. That was a big mistake on my part, but I'm leaving it because it happened and shouldn't be ignored.
Last edited on
Ok, Yawzheek's suggestion was ok for the most part until I only enter the invalid value of temperature and it even display "Invalid windspeed and temperature" while I wanted it only to display "Invalid temperature". Also, the code of Abstraction was way too advance for my class now. Any other simple way only using Else-If and operator?
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
#include <iostream>
#include <cmath>
using namespace std;

int main ()
	
{
	double temp = 0.0;
	double windspeed = 0.0;
	cout << "Enter the temperature and the wind speed: ";
	cin >> temp >> windspeed;
	
	
	if ((temp > -58) && (temp < 41) && (windspeed >= 2))
		cout << "The wind-chill temperature is " 
		     << 35.74 + (0.6215 * temp) - (35.75 * pow (windspeed, 0.16)) 
		         + (0.4275 * temp * pow (windspeed, 0.16)) << endl;
	
	if (windspeed < 2)
		cout << "Invalid wind speed" << endl;
	if (temp >= 41 || temp <= -58)
		cout << "Invalid temperature" << endl;
	
	return 0;
}
the code of Abstraction was way too advance for my class now.

If you've covered bool data types, there shouldn't be anything advanced about what I posted.
Line 8: Declares two bool variables. One to indicate the temperature is ok, and one to indicate the windspeed is ok.

The only thing even slightly tricky is lines 13 and 14. These are simply boolean expressions that set the corresponding boolean variable. The first one is equivalent to:
1
2
3
4
  if (temp > -58) && (temp < 41) 
    temp_ok = true;
  else
    temp_okay = false;

However, since (temp > -58) && (temp < 41) is a boolean expression, we simply assign the result to a boolean variable.

Lines 15,17,19 simply test the boolean variables.

If you haven't covered boolean data types yet, then I guess I understand your comment.



Topic archived. No new replies allowed.