Need help with Multiply and Divide operators

This is a discussion on Need help with Multiply and Divide operators within the C++ Programming forums, part of the General Programming Boards category; Alright, so I'm using switches and - Code: #include <iostream> using namespace std; int x; int a; int b; int ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    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;
    
    
        void Add()
    {
        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<<"1. Add\n";
        cout<<"2. Subtract\n";
        cout<<"3. Multiply\n";
        cout<<"4. Divide\n";
    
    
        cin>> c;
    
    
        switch (c) {
        case 1:
            Add();
            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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > cout << "Your final result is -\n" << x + a + b * g;
    Perhaps some extra ( ) ?

    cout << "Your final result is -\n" << (x + a + b) * g;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Quote Originally Posted by Salem View Post
    > 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. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    C++ follows basic rules of algebra.
    Also, global variables are bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 01-24-2008, 09:40 PM
  2. Addition using only multiply and divide
    By abachler in forum Contests Board
    Replies: 26
    Last Post: 09-19-2007, 01:40 AM
  3. multiply by 7
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-22-2006, 03:19 PM
  4. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 09:41 PM
  5. multiply int..
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-14-2002, 09:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21