-4

I am getting confused with a simple program of multiple if-else in c++. The code

is given below.

include<iostream.h>
void main()
{
int i;
cout<<"Enter the number:";
if(i==1)
{
cout<<"Sunday";
}

if(i==2)
{
cout<<"Monday";
}
else
{
cout<<" invalid input";
}
}

When I try to run this code ,the output shows this.

Enter the number:1
Sunday invalid key

So my question is why the output executing the Else part though the output is True..? Please help me . Thank You

indranil
  • 11
  • 1
  • 3
  • 2
    Where do you read in the input number? – DDan Aug 29 '15 at 07:16
  • 2
    [`void main` is wrong.](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – melpomene Aug 29 '15 at 07:17
  • Remember that after an `else` you can put *any* statement, it doesn't have to be a block-statement, so you can directly put a new `if` statement after the `else` like `else if (...)`. – Some programmer dude Aug 29 '15 at 07:19
  • 3
    code does not compile! Not to mention where do you get the word "key" as in _"invalid key"_ from? Should that not be _"invalid input"_. –  Aug 29 '15 at 07:33
  • 1
    [`include`](http://stackoverflow.com/q/2976477/995714) is also wrong, unless you're using Turbo C++ which is not a real C++ compiler – phuclv Aug 29 '15 at 07:42

6 Answers6

3

This is because you don't have "multiple if-else", really. You have a single if (without else), then another if. The two are independent. You probably wanted:

if(i==1)
{
    cout<<"Sunday";
}
else if(i==2)
{
    cout<<"Monday";
}
else
{
    cout<<" invalid input";
}

This makes sure the final else block only runs if none of the preceding conditions are met.

melpomene
  • 84,125
  • 8
  • 85
  • 148
1

First you check whether i equals one. If it is the case, "Sunday" is printed. The if statement is finished at that point. Afterwards you check (in a separate if statetement) whether i equals two, you print "Monday" if it is the case, or "invalid input" if it is not the case. To obtain the result you want, write

else if (i == 2)

to have the second if/else statement only executed if i is not 1.

Alternatively, you might want to use a switch statement.

switch(i)
{
    case 1:
        cout << "Sunday";
        break;
    case 2:
        cout << "Monday";
        break;
    default:
        cout << "invalid input";
        break;
}

But don't forget the breaks if using switch!

Michael Karcher
  • 3,803
  • 1
  • 14
  • 25
0

You have two different conditions. One is:

if(i==1) {
    cout<<"Sunday";
} // this statement ends here.

The other:

if(i==2) {
    cout<<"Monday";
} else {
    cout<<" invalid input";
}

The second is always going to result " invalid input" when i is not 2.

DDan
  • 8,068
  • 5
  • 33
  • 52
  • You need to show the full context of consisting of the guards for `i==1` and `i==2` for the code to be meaningful in the OP's scenario –  Aug 29 '15 at 07:40
  • The other if has nothing to do with the first – DDan Aug 29 '15 at 07:43
0

There are several bugs in this code. I explained and fixed it here-

#include<iostream.h>
void main()
{
    int i;
    cout<<"Enter the number:";
    cin >> i; //take the input number from the user
    if(i==1)
    {
        cout<<"Sunday";
    }    
    /* the i==1 and i==2 block will run separately unless you connect them with an else */
    else if(i==2) 
    {
        cout<<"Monday";
    }
    else
    {
        cout<<" invalid input";
    }
}
Dipu
  • 6,999
  • 4
  • 31
  • 48
0

You have to put else if, if you wanna have the right processing:

if(i==1)
    cout<<"Sunday";
else if(i==2)
    cout<<"Monday";
else
    cout<<" invalid input";

With else if, the second and the third condition are not processed, because the first one is alredy valid. In your code, it is first processed the code under the first condition, than because the input is not equal to 2, the code under the else is processed.

  • 1
    Apart from sans-braces (a bad habit in c/c++ by the way), how is this different to melpomene's answer, entered 14 minutes prior to your answer? –  Aug 29 '15 at 07:37
  • You are right, but I am new in the site and I spend a lot of time to edit the answer: when I was writing the code, no aswer was present yet. – alessandro mercadante Aug 29 '15 at 07:50
0

This is cause you have used two branching statement for same input

1. The first if() statement check if your value is equal 1 or not

if(i == 1)
    std::cout << "Sunday"; // here you have print "Sunday for 1

2. Then again you check your value with another if-else statement

if(i == 2)
  std::cout << "Mondey";
else
  std::cout << "invalid input";  // here you have print "invalid input" 
                                 //since i is not equal to 1
maruf
  • 599
  • 3
  • 6
  • 22