Simple If statement Issue

Hi there, I am having some trouble understanding why my program is not working here. I am trying to do some simple if statements here entering two numbers, but it does not seem to be working. I want my program to do a particular thing here.

First, enter two numbers. If the two numbers both entered are zero, display "good". If the second number entered is "0", display "Not cool" and if the first number is "0", display "Error!". My problem is this, if I enter 0 and 5, it should display "Error" but it's not working! For some reason, it's working if I enter 5 and 0 because since the second number is considered zero, it should display "Not cool".

After some digging around, I naturally found a possible (I could be wrong) solution because if I enter 0 and 5, the first if statement passes and therefore does not go to the other if statement, but I thought to my knowledge in-order for the first nested if statement to pass, they BOTH need to be true. Is this not the case? Thanks a bunch.

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

int main()
{
	int cool;
	int test;
	cin >> cool >> test;

	if (cool == 0)
	{
		if (test == 0)
		{
			cout << "Good\n";
		}
	}
	else if (test == 0)
	{
		cout << "Not cool\n";
	}
	else if (cool == 0) 
	{
		cout << "Error!\n";
	}
	else
	{

	}
	
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    int first ;
    int second ;
    std::cin >> first >> second ;

    if( first==0 && second==0 ) { /* both first and second are zero */ }
    else if( first==0 ) { /* first is zero, second is non-zero */ }
    else if( second==0 ) { /* second is zero, first is non-zero */ }
    else { /* both first and second are non-zero */ }
}
closed account (48T7M4Gy)
Whatever your program is supposed to be doing is still a mystery to me but it might make more sense if you add some prompts and messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
    int cool = 0;
    int test = 0;
    
    cout << "Enter cool test: ";
    cin >> cool >> test;
    
    cout << "cool = " << cool << '\n';
    cout << "test = " << test << '\n';
    
    if (cool == 0)
    {
        if (test == 0)
        {
            cout << "1. Good - cool and test are both zero\n";
        }
    }
    else if (test == 0)
    {
        cout << "2. Not cool \n";
    }
    else if (cool == 0)
    {
        cout << "3. Error!\n";
    }
    else
    {
        cout << "4. Nothing\n";
    }
    
    return 0;
}
My problem is this, if I enter 0 and 5, it should display "Error" but it's not working!


That would satisfy the first 'if'
1
2
3
4
5
6
7
	if (cool == 0)   // 0 == 0  gives true
	{
		if (test == 0)     // 5 == 0 gives false
		{
			cout << "Good\n";
		}  // no 'else' here
	}

Since the inner if is false, then nothing is output. None of the other if statements play a role, since the first was already satisfied.

You could add an else condition, but this might be better:

1
2
3
4
5
    if ( (cool == 0) && (test == 0) ) 
    {
        cout << "Good\n";
    } 
}


Combine the two conditions with a logical AND &&, then both tests must be true in order to output "Good". Otherwise processing with continue to evaluate the other tests.
Makes much more sense now guys, thanks a bunch!
Hello Bayan Khorshidi,

Think about what you are testing here. You have two variables and testing both will give a better result. Consider something like this:

1
2
3
4
5
6
7
8
9
10
11
12
	if (cool == 0 && test == 0)
	{
		std::cout << "\nGood\n";
	}
	else if (cool != 0 && test == 0)
	{
		std::cout << "\nNot cool\n";
	}
	else if (cool == 0 && test != 0)
	{
		std::cout << "\nError!\n";
	}


Not sure what you are trying to achieve here, but this is one possibility.

Hope that helps,

Andy
@Handy Andy, there seem to be some redundant, unnecessary tests there. If the first condition fails, there's no need to re-test both values in the remaining statements.
@Chervil,

I see your point now.

Andy
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

int main()
{
    int cool = 0;
    int test = 0;
    
    cout << "Enter cool test: ";
    cin >> cool >> test;
    
    cout << "cool = " << cool << '\n';
    cout << "test = " << test << '\n';
    
    if (cool == 0)
    {
        if (test == 0)
            cout << "1a. cool and test are both zero\n";
        else
            cout << "1b. cool zero and test non-zero\n";
    }
    else if(test==0)
        cout << "2a. cool non-zero and test zero\n";
    else
        cout << "2b. cool and test both non-zero\n";
    
    return 0;
}
Topic archived. No new replies allowed.