#include <iostream>
usingnamespace std;
int a, b, c;
int main()
{
cout << "Enter two numbers : ";
cin >> a >> b;
if (a + b)
{
c = a + b;
cout << " The Answer is " << c;
cout << "\n";
}
elseif (a - b)
{
c = a - b;
cout << " The Answer is " << c;
cout << "\n";
}
elseif (a*b)
{
c = a*b;
cout << " The Answer is " << c << endl;
cout << "\n";
}
elseif (a/b)
{
c = a / b;
cout << " The Answer is " << c << endl;
cout << "\n";
}
}
#include <iostream>
usingnamespace std;
int a = 0, b = 0, c = 0;
char op;
int main()
{
cout << "Enter number operator number separated by spaces : ";
cin >> a >> op >> b ;
if (op == '+')
{
c = a + b;
cout << " The Answer is " << c;
cout << "\n";
}
elseif (op == '-')
{
c = a - b;
cout << " The Answer is " << c;
cout << "\n";
}
elseif (op == '*')
{
c = a*b;
cout << " The Answer is " << c << endl;
cout << "\n";
}
elseif (op == '/' and b != 0)
{
c = a / b; // note integer divison
cout << " The Answer is " << c << endl;
cout << "\n";
}
}
What is the purpose of your if statements at lines 10, 16, 22 and 28?
Lines 11-15 will execute only if the sum of a and b is non-zero.
If the sum of a and b is non-zero, non of the remaining statements will execute because of the else on line 16.
Likewise, lines 17-21 will execute only if a and b are not the same.
Lines 23-27 will execute only if the product of a and b is non-zero.
Get rid of the if statements.
BTW, you're doing integer division on line 30. You won't get the result you're expecting.
You can replace the cascade of if statements with a switch control but it's not the end of the world to leave it as it is because they both do they same thing.
An additional else to capture op's outside +, -, * and / would be a good move. A message where dividing by zero wouldn't go astray either.
Using a double in calculating division overcomes the pitfalls of integer division. eg cout << 1.0 * a /b << endl;