Thread: My function's being dumb.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    My function's being dumb.

    Hi.

    I'm trying to write a C++ program for class that'll take two numbers and see if one is a multiple of the other. I'm doing this with a function and by repeatedly subtracting the bigger number by the smaller one until the value left over is "0" or lower.

    However, I am running into quite a stupid problem. I can get it to display the left over number and it'll either output "0" or lower letting me know that the code worked.

    As soon as I put in an "if" statement and a "cout << " to display either "True" or "False", it just... resets the number back to '0'? wtf?

    This is what I have so far:

    Code:
    //Page 274
    //Question 6.20
    
    
    #include <iostream>
    
    using namespace std;
    
    int multiple(int a, int b){
        int c;
        if(b>a){
                c = b;
        
                while(c>0){
                           c-=a;
                }
        }else if(b<a){
                c = a;
                
                while(c>0){
                           c-=b;
                }
        }
        
        return (c);
        
    }
    
    int main(){
        int numone, numtwo;
        int ismultiple;
        
        cout << "Enter a number: ";
        cin >> numone;
        cout << "Enter another number: ";
        cin >> numtwo;
        
        ismultiple = multiple(numone, numtwo);
        
        if(ismultiple=0)cout << "True.\n";
        
        cout << ismultiple << endl;
     
        return 0;   
    }
    Now comment out that "if(ismultiple=0)cout << "True.\n";" line and the code works. It displays the right number.

    Am I doing something wrong?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    if(ismultiple==0)cout << "True.\n";
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    On your multiple function, what gets returned when a==b?
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    'c' does.

    It starts off with 'c' being assigned the bigger number, and then it's subtracted by the smaller number until it's 0 or less.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Is that correct?
    What is neither number is bigger?
    Is a number considered to be a multiple of itself?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Yart View Post
    'c' does.

    It starts off with 'c' being assigned the bigger number, and then it's subtracted by the smaller number until it's 0 or less.
    As written, it will just return a random number, likely 0 or the value of a variable from a previously called and returned function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Quote Originally Posted by King Mir View Post
    As written, it will just return a random number, likely 0 or the value of a variable from a previously called and returned function.
    The thing is though, it's not.

    Here's some output:

    Code:
    Enter a number: 3
    Enter another number: 5
    0
    Now if I remove that one if statement with the cout in the end, it looks like this:

    Code:
    Enter a number: 3
    Enter another number: 5
    -1
    Now I want to take that -1 and go "Oh! It's false!", anything below 0. If it's 0 though, then go "Oh! It's true!"



    Why would that one practically irrevalent line screw up the entire thing? It's not changing anything...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Yart
    The thing is though, it's not.

    Here's some output:
    Dino asked "what gets returned when a==b?" and I notice that your sample output deals with another case.

    Quote Originally Posted by Yart
    Now I want to take that -1 and go "Oh! It's false!", anything below 0. If it's 0 though, then go "Oh! It's true!"
    I suggest that you rename multiple to isMultiple (or is_multiple), and change its return type to bool. If you do not actually need to use a loop for educational purposes, use an arithmetic operator related to division.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Am I missing something here? Surely you just need to use the % operator?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Yart View Post
    Now I want to take that -1 and go "Oh! It's false!", anything below 0. If it's 0 though, then go "Oh! It's true!"

    Why would that one practically irrevalent line screw up the entire thing? It's not changing anything...
    NeonBlack already gave you the answer in the very first reply.
    You're using = (assignment) instead of == (comparison).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM