I don't want to do your homework, but let me help you as much as possible...
In each of the following examples, only ONE of the "cout" statements will execute:
Example 1) The IF statement:
Code:
int x = 1;
if(x == 1)
{
cout << "X is equal to one." << endl;
}
In Example 1, the "cout" statement will execute.
Example 2) The IF / ELSE statement:
Code:
int x = 0;
if(x == 1)
{
cout << "X is equal to one." << endl;
}
else // This implies X is NOT equal to one, therefore the IF condition was not met
{
cout << "X is NOT equal to one." << endl;
}
In Example 2, the "cout" statement within the ELSE block will execute.
Example 3) The IF / ELSE IF / ELSE statement:
Code:
int x = 0;
if(x == 1)
{
cout << "X is equal to one." << endl;
}
else if(x == 0) // A second logical test, there can be as many ELSE-IF statements as you need...
{
cout << "X is equal to zero." << endl;
}
else
{
cout << "X is not equal to one, and X is not equal to zero." << endl;
}
In Example 3, the "cout" statement within the ELSE IF block will execute.
Another thing you need to consider in your program, is what to do if the user's input isn't valid. We won't worry about what number they enter, but what about the operand they select? You have to ask yourself some questions about your program. Does the user know HOW to enter the operand? Do they type "add"? Do they simply enter the '+' char? You can add a level of protection against unwanted input with the following code:
Code:
char choice;
cout << "Would you like to add, subtract, multiply or divide?" << endl
<< "Enter [+]-add [-]-subtract[*]-multiply [/]-divide then press [Enter]" << endl;
while ((choice != '+') && (choice != '-') && (choice != '*') && (choice != '/'))
{
cin >> choice;
}
The above continues to execute if the user enters something that is not what you specify in the WHILE statement.
BTW!!!
I'm not sitting in front of a computer with a compiler on it right now, so if anything doesn't work, please, nobody flame me! It's late and I have to get up in three hours to go to work!