-3

Here is what I have coded so far:

#include <iostream>
using namespace std;

    int main()
    {
      int num;
      cout << "Enter a month(as a number): ";
      cin >> num;
      if (num < 3 || num > 11)
      {
        cout << "Happy Winter\n";
      }
      else if (num > 7 || num < 9)
      {
        cout << "Happy Summer\n";
      }
      else if (num > 3 || num < 7)
      {
        cout << "Happy Spring\n";
      }
      else
      {
        cout << "Happy Fall\n";
      }
      return 0;
    }

My program is not printing things how it is supposed to. My progrma is supposed to print: "Happy Winter" if it is strictly before 3 or strictly larger than 11, "Happy Spring" if it is 3 or greater, but strictly before 7, and "Happy Summer" if it is 7 or greater, but strictly before 9, and "Happy Fall" otherwise.

but for some reason it only prints "Happy Summer" no matter what number i input. the only line that works is the first if statement i coded. I'm not sure what I am doing wrong but i think it has to do with my my operations in the else if statements.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
wzrd
  • 25
  • 3

1 Answers1

4

Take a look again at this, it's true if the number is less than 9 or greater than 7:

(num > 7 || num < 9)

That's all numbers.

You should be using and(&&) instead, the same is the case for your (num > 3 || num < 7).

Also note that your if statements miss out 3, so it's being classed as Fall. This is easy to miss because you're only strictly using only less than and greater than, rather than less/greater than or equal to.

Perhaps look at this:

if (num >= 3 && num <= 5) {
    cout << "Happy Spring\n";
} else if (num >= 6 && num <= 8) {
    cout << "Happy Summer\n";
} else if (num >= 9 && num <= 11) {
    cout << "Happy Fall\n";
} else {
    cout << "Happy Winter\n";
}

This specifically looks at months 3-5, then 6-8 then 9-11 and then the rest.

You should also consider reading Why is "using namespace std" considered bad practice?

Nick is tired
  • 6,860
  • 20
  • 39
  • 51