Nested If Else Shows Different Output

Hello everyone, I was working on a coding tutorial based on a question.
Question:
1. User is asked to enter any number.
2. If user key in number '5', a message will appear say "5 is no included."

This is my program//it turns out well
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int i=0;i<100;i++)
	{
	cout<<"Enter a number: "<<endl;
	cin>>number;
	cout<<endl;

	if(number==5)
	{
		cout<<"Hey! You are not supposed to enter 5!"<<endl;
		cout<<"Enter another number: "<<endl;
		cin>>number;
		cout<<endl;
	}


	}


If I put Else{enter another number}=====Whenever I enter 5, the message of "not suppose to enter 5" is gone. Like:
if(number==5)
{
cout<<"Hey! You are not supposed to enter 5!"<<endl;
/*cout<<"Enter another number: "<<endl;
cin>>number;*/
cout<<endl;
}
else
{
cout<<"Enter another number: "<<endl;
cin>>number;

}


Why?


Please show the whole program that does not work as you think. It is not clear from your description what is wrong.
Last edited on
Which Compiler are you using?
It's working fine in all the Compiler I have used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
    int number = 0;
    for (int i=0;i<100;i++){
                cout<<"Enter a number: "<<endl;
                cin>>number;
                cout<<endl;

            if(number==5){
                cout<<"Hey! You are not supposed to enter 5!"<<endl;
                cout<<"Enter another number: "<<endl;
                cin>>number;
                cout<<endl;
            }else{
                cout<<"Enter another number: "<<endl;
                cin>>number;
            }


    }
}



Try the above code. It should work fine.

Last edited on
Thanks. the problem is solved:)
Topic archived. No new replies allowed.