arg...function calls

This is a discussion on arg...function calls within the C++ Programming forums, part of the General Programming Boards category; im having problems with my function calls and relevant operators...ive been working on this for hours but i cant get ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    arg...function calls

    im having problems with my function calls and relevant operators...ive been working on this for hours but i cant get the result of my function to be recognized by the main function. this is where im at:
    Code:
    int remainder;
    int multiple (int x, int y)
    {
         
            remainder = x % y;
    
    return remainder;
    }
    
    int choice;
    
    int main ()
    {
    int x;
    int y;
    do{   
        
        cout << "Enter first integer (small): ";
        cin >> y;
        cout << "Enter second integer (larger): ";
        cin >> x;
        cout << "Is the first a multiple of the second?\n";
        if (remainder > 0)
            cout << "No." << multiple (x,y) << "\n";
        else
            cout << "Yes." << multiple (x,y) << "\n";
        
        cout << "Press 1 to try again, any other key to quit: ";
        cin >> choice;
    } while (choice == 1);
    
    return 0;
    }
    it just tells me yes everytime with a zero or one at the end(and i dunno how to remove that either!), so everything else works, even the do-while statement

    any small hints or nudgings in the right direction will help, thank you.
    Last edited by Salem; 09-23-2004 at 02:43 PM. Reason: tag! - you're it - does no-one read the READ BEFORE POSTING post?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > if (remainder > 0)
    This isn't a function call

    This is
    if (remainder(x,y) > 0)
    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
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> if (remainder > 0)

    what is the value of remainder when your program runs? step through it line by line from the begging. The function has to be called to give remainder a value.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Please use code tags, makes it much easier for us to read!

    Remember, the function will run at exactly the point you want it to. The way your logic is set up, it asks what remainder is BEFORE you figure out what it should be, then you have it print out yes or no followed by the value of remainder.

    I think what you want is something like this:
    Code:
    do{   
        
        cout << "Enter first integer (small): ";
        cin >> y;
        cout << "Enter second integer (larger): ";
        cin >> x;
        remainder=multiple(x,y);
        cout << "Is the first a multiple of the second?\n";
        if (remainder > 0)
            cout << "No because the remainder is " << remainder << "\n";
        else
            cout << "Yes because the remainder is " << remainder << "\n";
        
        cout << "Press 1 to try again, any other key to quit: ";
        cin >> choice;
    } while (choice == 1);

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Use PJYelton's answer - you have too many remainders in your code
    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.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    ohhh, ok...tricky stuff...

    so when i used 'if (remainder > 0)' it was just returning the result of the modulus, but instead i needed to call the actual function name.

    alright, well thanks for the quick responses, and i'll take the time to peruse the FAQ while im here

    p.s. code tags are my friends...code tags are my friends...code tags are my friends...
    Last edited by 2fastwrx; 09-23-2004 at 10:20 PM.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by 2fastwrx
    so when i used 'if (remainder > 0)' it was just returning the result of the modulus, but instead i needed to call the actual function name.
    Actually, no, it was getting the current value of remainder (which could be anything because you didn't initialize it to anything). The modulus was never actually performed until later. Only when you call the function does the code inside the function get executed, and the modulus is actually done.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    gotcha...so my custom functions arent executed until main tells them to. and without the function call written correctly (using the name and arguments...must drill that into brain) the functions are just worthless code

  9. #9
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Be sure if you dont have arguements to use the ()'s at the end that can be a headache sometimes if you forget.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 12
    Last Post: 10-16-2008, 02:49 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM

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