Thread: If function is executed without it's condition is true

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Question If function is executed without it's condition is true

    Hi!
    I am trying to write a simple program that produces different outputs based on entered age of two different users.
    Program should tell who is older and behave different if both users are older than 100.

    Here is my program:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int user1;
        int user2;
        cout << "User1, how old are you:" << endl;
        cin >> user1;
        cout << "User2, how old are you:" << endl;
        cin >> user2;
        if (user1 > user2 )
        {
            if  ((user1 && user2) <= 100)
                {
                 cout << "User1 is older!" << endl;
                }
        }
        else if ( user1 < user2 )
        {
            if ((user1 && user2) <= 100)
            {
             cout << "User2 is older!" << endl;
            }
        }
        else if ( (user1 > 100) && (user2 > 100) )
        {
            cout << "Both users are over 100 years old!" << endl;
        }
        else
        {
            cout << "Users are peers!" << endl;
        }
        cin.get();
        cin.ignore();
    }
    When I enter that user1 is 101 and user2 is 101, output is "Both users are over 100 years old!".

    But when I enter that user1 is 101 and user2 is 105, it gives output that "User2 is older!", instead of "Both users are over 100 years old!", what I actually want it to do.

    It seems that nested if function is executed without it's condition is evaluated true.

    Code:
     else if ( user1 < user2 )
        {
            if ((user1 && user2) <= 100)
            {
             cout << "User2 is older!" << endl;
            }
        }
    I also tryed without nesting, like this:

    Code:
    else if ( user1 < user2 ) && ((user1 && user2) <= 100)
        {
             cout << "User2 is older!" << endl;
        }
    But it didn't work.

    Why program executes this when both users are obviously more than 100, can someone explain me, please?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But it didn't work.

    Why program executes this when both users are obviously more than 100, can someone explain me, please?
    Because (user1 && user2) is always less than 100.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by jimblumberg View Post
    Because (user1 && user2) is always less than 100.

    Jim
    Why?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because you are using the logical AND so if user1 and user2 are equal it evaluates to 1 otherwise it evaluates to zero. You should be using something like
    Code:
    if((user1 <= 100 && user2 <= 100) && (user1 < user2))
    {
       ...

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Ok, thanks!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You could also just re-arranges some of those if-else clauses to get the right result. Check for the "both users are over 100" first, before doing the other checks.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    I solved it this way

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int user1;
        int user2;
        cout << "User1, how old are you:" << endl;
        cin >> user1;
        cout << "User2, how old are you:" << endl;
        cin >> user2;
        if ( !((user1 > 100) && (user2 > 100)) )  // Execute, except if user1 and user2 are not bigger than 100
        {
            if ( user1 < user2 )
            {
                cout << "User2 is older!" << endl;
            }
            else if ( user1 > user2 )
            {
                cout << "User1 is older!" << endl;
            }
            else
            {
                cout << "Users are peers!" << endl;
            }
        }
        else                                             //otherwise execute this
        {
            cout << "Both users are over 100 years old!" << endl;
        }
        cin.get();
        cin.ignore();
    }
    Everything is more clear now, thanks!

  8. #8
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I propose a solution a bit simpler:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int user1;
        int user2;
        cout << "User1, how old are you:" << endl;
        cin >> user1;
        cout << "User2, how old are you:" << endl;
        cin >> user2;
        cin.ignore();
    
        if (user1 > 100 && user2 > 100)
        {cout << "Both users are over 100 years old!" << endl;}
        else 
        { 
            if (user1 > user2)
            {cout << "User1 is older!" << endl;}
            else {cout << "User2 is older!" << endl;}
        }
    
        cin.get();
    }

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Well, your solution is much more elegant, but I just don't like that program says that User2 is older when they are both of same age, so I added one more function to your solution:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {    int user1;
         int user2;
            cout << "User1, how old are you:" << endl;
            cin >> user1;
            cout << "User2, how old are you:" << endl;
            cin >> user2;
            cin.ignore();
            if (user1 > 100 && user2 > 100)
            {cout << "Both users are over 100 years old!" << endl;}
            else
            {
                if (user1 > user2)
                {cout << "User1 is older!" << endl;}
                else if (user1 < user2)
                {cout << "User2 is older!" << endl;}
                else {cout << "Users are peers!" << endl;} //The third possibility
            }
            cin.get();
    }
    Now it's perfect
    Last edited by Cjof; 03-06-2013 at 04:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function is not executed....
    By enakta13 in forum C Programming
    Replies: 4
    Last Post: 09-25-2012, 09:57 PM
  2. If function - or condition with character input
    By Luponius in forum C Programming
    Replies: 5
    Last Post: 04-17-2011, 04:43 PM
  3. What is the true address of a function ptr?
    By Raigne in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 12:24 PM
  4. Help with a condition function
    By swanley007 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2006, 02:20 PM
  5. How do I use the char & true function?
    By Zopyrus in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2003, 02:51 PM