if/else if

how can I ask the windy question should I change the structure or can I use more expressions in the if statement Can I get some help please the program asks
. Write a C++ program that asks for the current temperature in Fahrenheit and also asks the user if it is windy outside. In regards to wind, if it is windy, it should tell them to wear a jacket. If the temperature is less than 0 degrees, it should tell the user to stay home. If it is less than 20 degrees, it should tell them to wear a heavy coat. If it is less than 45 degrees it should tell them to wear a coat. If it is less than 70 degrees, it should tell them to wear a jacket. Otherwise, it should tell them that they do not need a jacket.

Please use good common sense when designing your decision structures. Does it make sense to wear a jacket and a coat at the same time? Test your program with a variety of test values to confirm it behaves as expected.


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

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

	cout << " What is the temperature outside in Fahrenheit? ";
	cin >> temp;
	cout << " Answer the following question\n with Y for Yes or N for No\n";
	cout << " Is it windy outside?\n ";
	cin >> windy;

	if (temp < 0) 
	{
		cout << " Stay Inside !!" << endl << endl;
	}
	else if (temp >= 0 && temp < 20)
	{
		cout << " Wear a heavy coat !!" << endl << endl;
	}
	else if (temp >= 20 && temp < 45)
	{
		cout << " Wear a coat" << endl << endl;
	}
	else if (temp >= 45 && temp < 70)
	{
		cout << " Wear a jacket ! " << endl << endl;
	}
	else
	{
		cout << " You do not need a jacket." << endl << endl;
	}


	system("pause");
	return 0;
}
 Thanks for any help
What is the specific issue? Is it the windy condition?
Yes Im have a problem with asking both the temp and is it windy if it is windy, it should tell them to wear a jacket. but on the same hand if the temp is within a certain temp it will tell them to wear a heavy coat and you cant do both plus if it isn't windy it output you don't need a jacket as the program states " Please use good common sense when designing your decision structures. Does it make sense to wear a jacket and a coat at the same time? Test your program with a variety of test values to confirm it behaves as expected." i've tried and && and || so the code you see is the basic until I can figure adding the Windy decision structure


Any suggestion
Thanks for responding
From how I see it, the condition windy doesn't matter unless the temperature is greater than 70 and it is windy so I included lines 32-35. Hope this helps.

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

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

	cout << " What is the temperature outside in Fahrenheit? ";
	cin >> temp;
	cout << " Answer the following question\n with Y for Yes or N for No\n";
	cout << " Is it windy outside?\n ";
	cin >> windy;

	if (temp < 0 ) 
	{
		cout << " Stay Inside !!" << endl << endl;
	}
	else if (temp >= 0 && temp < 20)
	{
		cout << " Wear a heavy coat !!" << endl << endl;
	}
	else if (temp >= 20 && temp < 45)
	{
		cout << " Wear a coat" << endl << endl;
	}
	else if (temp >= 45 && temp < 70)
	{
		cout << " Wear a jacket ! " << endl << endl;
	}
	else if(temp > 70 && (windy == 'Y' || windy == 'y'))
	{
	    cout<<"Wear a jacket ! "<<endl;
	}
	else
	{
		cout << " You do not need a jacket." << endl << endl;
	}


	system("pause");
	return 0;
}
Last edited on
another problem is when windy == N it just goes to Stay inside no matter what the temp is?


// Albert Grennan
// Assignment 4.1
/*
Write a C++ program that asks for the current temperature in Fahrenheit and also asks
the user if it is windy outside. In regards to wind, if it is windy, it should tell them to wear a jacket.
If the temperature is less than 0 degrees, it should tell the user to stay home.
If it is less than 20 degrees, it should tell them to wear a heavy coat. If it is less than 45 degrees it
should tell them to wear a coat. If it is less than 70 degrees, it should tell them to wear a
jacket. Otherwise, it should tell them that they do not need a jacket.
*/

#include<iostream>
using namespace std;

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

// asking user the temp and if its windy
cout << " What is the temperature outside in Fahrenheit? ";
cin >> temp;
cout << " Answer the following question\n with Y for Yes or N for No\n";
cout << " Is it windy outside?\n ";
cin >> windy;

// decisions & output
if ((temp < 0) && (windy == 'Y') ||( windy == 'N'))
{
cout << " Stay Inside !!" << endl << endl;
}
else if ((temp >= 0 && temp < 20) && (windy == 'Y') || (windy == 'N'))
{
cout << " Wear a heavy coat !!" << endl << endl;
}
else if ((temp >= 20 && temp < 45) && (windy == 'Y') || (windy == 'N'))
{
cout << " Wear a coat" << endl << endl;
}
else if (temp >= 45 && temp < 70)
{
cout << " Wear a jacket ! " << endl << endl;
}
else if (temp > 70 && (windy == 'Y'))
{
cout << "Wear a jacket ! " << endl << endl;
}

else
{
cout << "You do not need a jacket." << endl << endl;
}


system("pause");
return 0;
}
Never mind the above worked I just had to change the ()
Thanks for your help nicholasjb1996 !!
I think Nicholasjb1996 is right: whether it's windy or not actually makes no difference. You're going to instruct the user to wear some protective clothing for any temperature below 70. Above 70, there's no need for a jacket even if it is windy - at least in my mind.

Hmm. Perhaps you should have them wear a jacket if it's windy and, say, between 70 and 75. Just a thought.

Also, consider this code:
1
2
3
4
5
if (temp < 0) 
{
	cout << " Stay Inside !!" << endl << endl;
}
else if (temp >= 0 && temp < 20)

You actually don't need to test for temp >= 0. If you get to the else part then the temp isn't less than 0, which means it must be greater than or equal to zero. So I think the decision ladder is:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (temp < 0) {
	cout << " Stay Inside !!" << endl << endl;
} else if (temp < 20) {
	cout << " Wear a heavy coat !!" << endl << endl;
} else if (temp < 45) {
	cout << " Wear a coat" << endl << endl;
} else if (temp < 70) {
	cout << " Wear a jacket ! " << endl << endl;
} else if (temp < 75 && (windy == 'Y' || windy == 'y')) {
	cout << "Wear a jacket!\n\n";
} else {
	cout << " You do not need a jacket." << endl << endl;
}

Notice that I've used \n instead of endl. Endl is very inefficient so I recommend getting into the habit of using it only when you really need to flush the stream.
Topic archived. No new replies allowed.