This is going to be utterly stupid I am sure of it.. The code the way it is written is not allowing for the else statement and is always evaluating to 'true'
however my compiler says there is no errors found.
#include <iostream>
usingnamespace std;
bool IsEven()
{
// if x % 2 == 0, 2 divides evenly into our number
// which means it must be an even number
int x;
if (x % 2 == 0)
returntrue;
elsereturnfalse;
}
main()
{
cout << "What will x be?" << endl;
int x;
cin >> x;
if (IsEven())
cout << "Your number was even!" << endl;
else
cout << "Your number was odd!" << endl;
}
The x in your main function has nothing in common with the x you declared in your isEven function.
The x in your IsEven function is uninitialized and therefore causes undefined behavior that you can't rely on.
in other words,
I would pass your x variable into your function as a parameter instead:
IsEven function doesnt make any sense til you put parameter on it for passing the reference on x , on main function, which reads input on this program.