If And Else Statements Not Working

Oct 4, 2009 at 5:04pm
Hey guys I am trying to use If and If Else Statements on two numbers to get different outcomes.
Like the user enters two numbers and if they are even it adds them and if they are odd it subtracts them and if we have on of each it divides and multiplies them. My problem is that the If statements don't seem to work the code just displays all the math regardless of whether the numbers are even or odd. And when ever I try to use a Else statement it wont connect to the previous if statement.

Heres My Code For Reference.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
//Names Statement
char question[] = "Please, enter your first name: ";
char greeting[] = "Hello And How Nice To Meet You, ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!" << endl;


//Input The Numbers Statement
long number1,number2;
cout << "enter first whole number: " << endl;
cin >> number1;
cout << "enter second whole number: " << endl;
cin >> number2;

//The Variable Statement For Even Numbers

if (number1 % 2 == 0) (number2 % 2== 0);
{
cout << number1 <<" + "<<number2<<" = "<< number1+number2 << "\n\n " << endl;
}

//The Variable Statement For Odd Numbers

if (number1 % 2 == 1) (number2 % 2 == 1);
{
cout << number1 <<" - "<<number2<<" = "<< number1-number2 << "\n\n " << endl;
}

if (number1 % 2 == 0) (number2 % 2 == 1);
{
cout << number1 <<" / "<<number2<<" = "<<number1/number2 << "\n\n " << endl;
cout << number1 <<" x "<<number2<<" = "<<number1*number2 << "\n\n " << endl;
}

if (number1 % 2 == 1) (number2 % 2 == 0);
{
cout << number1 <<" / "<<number2<<" = "<<number1/number2 << "\n\n " << endl;
cout << number1 <<" x "<<number2<<" = "<<number1*number2 << "\n\n " << endl;
}

return 0;
}
Oct 4, 2009 at 6:01pm
Your "if" statements are not coded properly. The snyntax is entirely wrong.
Oct 4, 2009 at 6:35pm
if (number1 % 2 == 1) (number2 % 2 == 0);
- Remove the final semicolon
- If this means 'If number1 etc. and number2 etc. are both true then..." replace the middle )( with && -logical AND-
- If this means 'If at least one of number1 etc. and number2 etc. is true then..." replace the middle )( with || -logical OR-
Last edited on Oct 4, 2009 at 6:35pm
Topic archived. No new replies allowed.