Question about If, Else statements

Write a program that accepts two numbers – temperature and day of week (if it is Sunday 1, otherwise 2. Display the following, if:

temperature < -10 and Sunday – Stay home.

temperature < -10 and not Sunday – Stay home, but call work.

temperature <= 0 (but >= -10) – Dress warm.

temperature > 0 and Sunday – Play hard.

temperature > 0 and not Sunday – Work hard.

Hint: Use if and else if statements

*** Sample output

Please enter temperature: .5

Is it Sunday(Yes=1, No=2)? 1

Play hard.

Please enter temperature: -8

Is it Sunday(Yes=1, No=2)? 2

Dress warm.

Ok sorry guys I am a complete noobie, but here is my question that I am struggling with. It seems like I have got it mostly right, but when I try to input .5 it completely skips me being able to input the day of the week and just outputs work hard. I can put in a whole number and it seems to work fine. Is there something in particular I am missing here?

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 <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    int temperature;
    char weekday;

  

   cout << "Please enter temperature: ";
   cin >> temperature;
   
   cout << "\n";

   cout << "Is it Sunday(Yes=1, No=2)? ";
   cin >> weekday;
   
   cout << "\n";
   
   if ((temperature < -10) && (weekday == 1)) {
                    cout << "Stay home" << endl;
       
   }else if ((temperature < -10) && (weekday != 1)) {
          cout << "Stay home, but call work" << endl;
       
   }else if ((temperature <= 0) && (temperature >= -10)) {
          cout << "Dress warm" << endl;
       
   }else if ((temperature > 0) && (weekday == 1)){ 
           cout << "Play hard" << endl;
           
   }else if ((temperature > 0) && (weekday != 1)) {
       cout << "Work hard" << endl;
       }
       
    system("PAUSE");
    return EXIT_SUCCESS;

}
You've defined temperature as an int. Sounds like you should be defining it as a floating point type (float or double) if temperatures with decimals are going to be entered?
Since you are inputting .5 as the temperature, the variable temperature cannot be of type int. Use double instead to allow the input of decimal values.


In the comparison (weekday == 1) weekday is a char but 1 is an integer. You should compare like with like, or at least types which are readily convertible. In this case, use the character '1' instead of the integer 1, that is, (weekday == '1')
Last edited on
Awesome, that looks like it should take care of it. Thank you all sooo much.
Topic archived. No new replies allowed.