Multiplying! PLEASE HELP!!!!!!!!!!!!!

This is a discussion on Multiplying! PLEASE HELP!!!!!!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; I have this Code #include <iostream.h> int mult(int x, int y); int main() { int x, y; cout<<"Please input two ...

  1. #1
    Unregistered
    Guest

    Exclamation Multiplying! PLEASE HELP!!!!!!!!!!!!!

    I have this Code

    #include <iostream.h>

    int mult(int x, int y);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    return 0;
    }
    int mult(int x, int y)
    {

    return x*y;
    }

    it works fine but how would i be able to get it to say Pick More numbers to multiply and then do the same job as the code above ?
    Please help
    ICE

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    One method would be to use default paramaters in your function, for example:
    Code:
    int mult(int x, int y, int z = 1)
    And now Z is a default paramater, which means by default its 1, but you can also change it. So if you were to return x*y*z with your function being called: mult(5,5) you would get 25. However, you could also use mult(5,5,5) and you would get 125.

    I hope you're following this, its sort of ugly (English) wise, but it shouldn't be too hard. Your new code would look like this:

    #include <iostream.h>

    int mult(int x, int y, int z = 0);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    cout<<"Please input three numbers to be multiplied: ";
    cin>>x>>y>>z;
    cout<<"The product of these three numbers is "<<mult(x, y, z);
    return 0;
    }
    int mult(int x, int y, int z)
    {
    return x*y*z;
    }
    "Where genius ends, madness begins."
    -Estauns

  3. #3
    Unregistered
    Guest
    with a control loop. There are three types initialized by the keywords for, while, and do/while. There usage should be well demonstrated in any C++ textbook you have available or in the tutorial at the home page of this board.

  4. #4
    Unregistered
    Guest

    Question Dunt work

    thers errors :
    Im a newbie
    so i dont know how to fix em
    ICE

  5. #5
    Unregistered
    Guest

    Exclamation HELP ME AGAIN

    #include <iostream.h>

    int mult(int x, int y, int z = 0);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    cout<<"Please input three numbers to be multiplied: ";
    cin>>x>>y>>z;
    cout<<"The product of these three numbers is "<<mult(x, y, z);
    return 0;
    }
    int mult(int x, int y, int z)
    {
    return x*y*z;
    }

    thats my code
    whats wrong and what will be the final code ?

  6. #6
    Unregistered
    Guest

    Wrong

    Whoops
    that was the wrong code
    #include <iostream.h>

    int mult(int x, int y);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    return 0;
    }
    int mult(int x, int y)
    {

    return x*y;
    }

    thasts the wright 1
    the codes fine i just want it to say pick another number then repeats the code again!

  7. #7
    Unregistered
    Guest
    you could do this:

    add the line

    char more = 'y';

    after you declare x and y. Then use a while loop with the conditional being (more == 'y') and after the second cout statement put a third cout telling the user to enter a lower case y if they wish to do more multiplication problems and anything else if they wish to stop. Accept user input using cin placing the input in more. Used in this way the variable more is sometimes called a flag to indicate which option to follow. Use of control loops and flags is very common in programming and is a technique you should become comfortable with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying chars
    By orjanb314 in forum C Programming
    Replies: 8
    Last Post: 11-20-2008, 08:58 AM
  2. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 05:53 PM
  3. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 01:34 PM
  4. Algorithm for multiplying linked lists?
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 11:59 AM
  5. multiplying by 321 without using * or / operators
    By Silvercord in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2003, 05:47 AM

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