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?