if-then syntax help.

There is somekind of syntax error going on with my nested if statments, the code compiles correctly but instead of outputing one of the choices i.e. enter 91 and get "Visit a neghbor." it lists all of the message outputs in a line if you input a number greater then 90. if you input a number less then 90 it doesnt output any message. ive been working on this for the last 3 hours :(.

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

int main()
{
	int temperature;
	cout << "Enter the temperature in your room.";
	cin >> temperature;

	if (temperature >= 90)
	{
		cout << "Visit a neighbor.";
		if (temperature >=80<=89)
		{
			cout << "Turn on air coniditioning.";
			if (temperature >=70<=79)
			{
				cout << "Do nothing.";
				if (temperature >=56<=69)
				{
					cout << "Turn on heat.";
					if (temperature <= 55)
					{
						cout << "Visit neighbor.";
					}
				}
			}
		}
	}

	return 0;
}



I am at a loss and have no idea where to go from here in order to fix it.
As it stands, all of your if statements are chained, so if you don't enter the first one (if temparture >= 90), then you won't check any of the others. Also, your syntax is screwed up on the middle ones, if you want to check if something is within a range you must do it like this:

if(var <= max && var >= min)
First of all, like firedraco said - your syntax is wrong. For example to check if temperature is between value '70' and '90' its:

if(temperature >=70 && temperature <= 90)

As for the code this would make a lot more sense:

1
2
3
4
5
6
7
8
9
10
if (temperature >= 90)
   {
    cout<<"Visit a neighbour";
   }
                
else if (temperature >=70 && temperature <= 90)
     {
      cout<<"Turn on air conditioning";
     }
else if ...


etc. etc.

What you're code is doing is basically saying if temperature>=90 output visit a neighbor AND if temperature is between 80 and 89 output turn on air conditioning AND if temperature ... so on and so forth.

I hope this makes sense to you.

Yes it all makes sense, thank you for the information. I kind of figured i was doing something wrong but was unsure.
Topic archived. No new replies allowed.