+ 2
why is it that when I input 'a' as '2' in this short program, it gives me 'no output'. is there anything wrong with the if-statement nesting?.
int main () { int a; cin >>a; if (a==1){ cout<<"a is 1\n"; if (a==2){ cout<<"a is 2"; }} return 0; }
3 Respuestas
+ 2
Your 'a==2' if statement is nested inside the 'a==1' if statement, so will only be reached if a=1 but will obviously never be true.
Where you have the two closing curly braces, you need to move one of them to close the first if statement.
if ( a == 1 ) {
cout << "a is 1";
}
if ( a == 2 ) {
cout << "a is 2";
}
+ 1
You're (a == 2) if statement is inside the (a == 1) if statement.
Yours:
if (a == 1)
{
cout << "a is 1";
if (a == 2)
{
cout << "a is 2";
}
}
How it should be:
if (a == 1)
{
cout << "a is 1";
}
if (a == 2)
{
cout << "a is 2";
}
+ 1
thanks guys..