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 :(.
#include <iostream>
usingnamespace 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:
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";
}
elseif (temperature >=70 && temperature <= 90)
{
cout<<"Turn on air conditioning";
}
elseif ...
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.