# Thread: Need help with Multiply and Divide operators

1. ## Need help with Multiply and Divide operators

Alright, so I'm using switches and -

Code:
```#include <iostream>

using namespace std;

int x;
int a;
int b;
int e = 100;
int f = 50;
int g = 10;
int h = 10;

{
cout <<"\n Added 100 to " << a + b + x<< "\n";
cout << "Your final result is -\n" << e + x + a + b;
}
void Subtract()
{
cout <<"\nSubtract 50 from " << x + a + b << "\n";
cout << "Your final result is -\n" << x + a + b - f;
}
void Multiply()
{
cout <<"\nMultiplied 10 with " << x + a + b << "\n";
cout << "Your final result is -\n" << x + a + b * g;
}
void Divide()
{
cout <<"\nDivided 10 to " << x + a + b << "\n";
cout << "Your final result is -\n" << x + a + b / h;
}

int main()
{
cout << "Please input a number: ";
cin >> x;
cin.ignore();

cout << "\nYou have entered " << x << ". Please hit ENTER to continue";
cin.get();

cout << "\nPlease enter two numbers to be added to " << x << " :";
cin >> a >> b;
cin.ignore();
cout << "\nThe total is - " << a + b + x << ". " << "Please hit ENTER to continue";

cin.get();

int c;

cout<<"2. Subtract\n";
cout<<"3. Multiply\n";
cout<<"4. Divide\n";

cin>> c;

switch (c) {
case 1:
break;
case 2:
Subtract();
break;
case 3:
Multiply();
break;
case 4:
Divide();
break;
default:
cout<<"Input not recognized. Quitting now\n";
break;

cin.get();
}

}```
Add and Subtract works well with no problem. But it is Multiply and Divide that is making my head scratch.

If I put 100 into x, a and b and use Multiply to multiply 300 by 10, it doesn't return 3000, it returns 1200...

For Divide, if I put the same number, 100, into x, a and b and use Divide to divide 300 by 10, it doesn't return 30, it returns 210...

Does anyone know how to fix this?

2. > cout << "Your final result is -\n" << x + a + b * g;
Perhaps some extra ( ) ?

cout << "Your final result is -\n" << (x + a + b) * g;

3. Originally Posted by Salem
> cout << "Your final result is -\n" << x + a + b * g;
Perhaps some extra ( ) ?

cout << "Your final result is -\n" << (x + a + b) * g;
It works! Thanks for the help

+1 to you.

4. C++ follows basic rules of algebra.