-4
 #include <iostream>
 using namespace std;
 int main()
 {
int n;
cin >> n;
if( 1<=n<=9)
    {
    switch(n)
        {
    case 1:cout<<"one"; break;
    case 2:cout<<"two"; break;
    case 3:cout<<"three"; break;
    case 4:cout<<"four" ;break;
    case 5:cout<<"five" ;break;
    case 6:cout<<"six" ;break;
    case 7:cout<<"seven"; break;
    case 8:cout<<"eight" ;break;
    case 9:cout <<"nine";

    }
  }
  else
    { cout<<"greater than nine"; }

}

the above code when i am running (or)compiling my else statement is not working for example if i am giving a number like 44 it is not displaying the statement in else that it is grater than nine but the if case is working nicely.

  • 1
    Your if condition is not properly specified. – Anon Mail May 18 '17 at 15:52
  • 2
    The expression `1<=n<=9` is equal to `(1 <= n) <= 9`, which means you compare a boolean value (the result of `1 <= n`) with a number. – Some programmer dude May 18 '17 at 15:52
  • `1<=n<=9` is not the correct way to specify multiple boolean conditions. You need `&&` like in most languages. – AndyG May 18 '17 at 15:52
  • 2
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 18 '17 at 15:53
  • can you tell me where to correct my if condition@AnonMail – Nikhil Sai May 18 '17 at 15:53
  • where you have "if (1 <= n <= 9)", you need to change it to something that will actually do what you want. – E.D. May 18 '17 at 15:54
  • "1 <= n <= 9" is equivalent to "(1 <= n) <= 9". if n = 44, then first we evaluate (1 <= 44), which evaluates to 1, so then we evaluate 1 <= 9, which evaluates to 1. – E.D. May 18 '17 at 15:55
  • ya i understood now , i am new to programming so i got confused – Nikhil Sai May 18 '17 at 16:02
  • 1
    Your condition should be like this if (n >= 1 && n <= 9) if you'are new with programming language and you don't know how to properly make a condition you can use this link / web site to learn http://www.cprogramming.com/tutorial/lesson2.html This lesson will help you to learn how to make a condition – Hicham Bouchilkhi May 18 '17 at 16:04

2 Answers2

3

You have a problem with your if condition.

It should be like this:

if (n >= 1 && n <= 9)

As it is now, it means

if ((1 <= n) <= 9)

Which will always evaluate to true.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
1

Try changing the condition to

if (1<=n && n<=9)
Ðаn
  • 10,934
  • 11
  • 59
  • 95