My function's being dumb.

This is a discussion on My function's being dumb. within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm trying to write a C++ program for class that'll take two numbers and see if one is a ...

  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
    435
    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
    Katy, Texas
    Posts
    2,309
    On your multiple function, what gets returned when a==b?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    435
    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,053
    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
    21,992
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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

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