/*Ask the user to enter an integer and tell whether if its positive or negative*/
#include <iostream>
usingnamespace std;
int main()
{
int num;
cout << "Enter an integer!" << endl;
cin >> num;
if ( 0 < num )
cout << "The integer is positive!" << endl;
else
cout << "The integer is negative!" << endl;
return 0;
}
True, it's neither positive or negative. But if a user is to enter 0, the program will just quit. It should at least print something like "The integer is zero".
Maybe this is what you want. on original code 0 was considered as negative.
1 2 3 4 5 6
if ( 0 < num )
cout << "The integer is positive!" << endl;
elseif (0 > num)
cout << "The integer is negative!" << endl;
else
cout << "The integer is 0!" << endl;
True, it's neither positive or negative. But if a user is to enter 0, the program will just quit. It should at least print something like "The integer is zero".
Maybe this is what you want. on original code 0 was considered as negative.
1 2 3 4 5 6
if ( 0 < num )
cout << "The integer is positive!" << endl;
elseif (0 > num)
cout << "The integer is negative!" << endl;
else
cout << "The integer is 0!" << endl;
maybe what you mean is the order of the variable like it has to write first? afterall, the if statement execute the true result inside the parentheses, so the order of the writing is not relevant (afaik). e.g.:
if (1 >= relevantVar) //if relevantVar is -3, then this statement is true