# My function's being dumb.

• 11-01-2009
Yart
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?
• 11-01-2009
NeonBlack
Code:

`if(ismultiple==0)cout << "True.\n";`
• 11-01-2009
Dino
On your multiple function, what gets returned when a==b?
• 11-01-2009
Yart
'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.
• 11-01-2009
NeonBlack
Is that correct?
What is neither number is bigger?
Is a number considered to be a multiple of itself?
• 11-01-2009
King Mir
Quote:

Originally Posted by Yart
'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.
• 11-01-2009
Yart
Quote:

Originally Posted by King Mir
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...
• 11-01-2009
laserlight
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.
• 11-02-2009
darren78
Am I missing something here? Surely you just need to use the % operator?
• 11-02-2009
cpjust
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!"

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