Problem with 'while' validation Y or N statement

For the weather program, validate that the temperatures are between -20 and 120 degrees and validate that the user has typed an acceptable answer in response to being asked if it is windy.Do not let them continue until an appropriate answer is received. I need help with the "while windy Y || N validating statement" If the user types the appropriate answer the program continues if not they get a "invalid entry" statement Id appreciate any help


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
#include<iostream>
using namespace std;

int main()
{
	// variables
	double temp;
	char windy; // Y or N

	/* asking user the temp and
	validating */
	cout << " What is the temperature outside in Fahrenheit? ";
	cin >> temp;

	while ((temp <= -20) || (temp >= 120))
	{
		cout << " Invalid entry !!! Please enter a temperature\n between -20 and 120 degrees\n";
		cin >> temp;
	}
	cout << "Is it windy? enter Y for yes and N  for no";
	cin >> windy;

	while ((windy != 'Y') || (windy != 'N'))
	{
		cout << " Invalid entry !!! Please enter Y or N";
		cin >> windy;
	}

	// decisions & output
	//If the temperature is less than 0 degrees tell the user to stay home .
	if (temp < 0)
	{
		cout << " Stay Home !!" << endl << endl;
	}
	//If it is less than 20 degrees, tell them to wear a heavy coat.
	else if (temp >= 0 && temp < 20)
	{
		cout << " Wear a heavy coat !!" << endl << endl;
	}
	//If it is less than 45 degrees tell them to wear a coat.
	else if (temp >= 20 && temp < 45)
	{
		cout << " Wear a coat" << endl << endl;
	}
	else if (temp >= 45 && temp < 70)
		//If it is less than 70 degrees,tell them to wear a jacket
	{
		cout << " Wear a jacket ! " << endl << endl;
	}
	else if (temp > 70 && windy == 'Y')
		///If it is greater than 70 degrees, amd windy tell them to wear a jacket 
	{
		cout << "Wear a jacket ! " << endl << endl;
	}

	else
		//Otherwise, tell them that they do not need a jacket.
	{
		cout << "You do not need a jacket." << endl << endl;
	}


	system("pause");
	return 0;
}
  
((windy != 'Y') || (windy != 'N'))

This will alway result in "true". Do you see why?
If windy == 'Y', it will result in (false || true), which is true.
If windy == 'N', it will result in (true || false), which is true.
If windy == 'X', it will result in (true || true), which is true.

You want &&, not ||.
windy doesn't equal Y AND windy doesn't equal N.
Last edited on
Thanks Ganado I understand why Thanks again
Topic archived. No new replies allowed.