Help with code.

I am trying to determine if a number is a multiple of another number. I have the user enter a number, then a second number to check if the second is a multiple of the first number entered.
I decided to divide the two numbers and check to see if the multiple number is whole or not. I round up the answer (if it's a decimal it isn't a multiple), and check to see if it is more than the second number entered or equal to it, then do output based on the result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <math.h>
using namespace std;

double number;
double multiple;
double num;
double num2;
bool factor;
bool multiples (bool); 

int main()
{
	cout << "Enter a number you want to find a multiple for." << endl;
	cin >> number;
	cout << "Enter another number to test if it is a mulitple of the first number." << endl;
	cin >> multiple;
	multiples(factor);
	if (factor = true)
	cout << multiple << " is a multiple of " << number << "." << endl;
	if (factor = false)
	cout << multiple << " is not a multiple of " << number << "." << endl;
	
	return 0;
}
		
	bool multiples (bool a)
{   
	num = number/multiple;
	num2 = ceil(num);
	if (num < num2)
		factor = false;
	if (num = num2)
		factor = true;
    return factor;
}


No matter what it wants to execute

1
2
if (factor = true)
	cout << multiple << " is a multiple " << number << "." << endl;

I debugged by entering 5 and 2, and factor will be false (since 5/2 = 2.5. 2.5 rounds up to 3, so 3 > 2), but as soon as it gets to that line it will change to true regardless.

Any idea what is causing this?
Thanks!

Last edited on
When checking if 2 values are equal to each other, or if a value is equal to true or false you need to use ==

Using = is for directly assigning a value to a variable. So fix that on line 19, 21, 33 etc
Ok thanks. lol I'm new to C++ , I figured it was something very simple.
Last edited on
Also, '%' is the modulus operator.

¿Why is your function taking a parameter if it would never be used?
¿Why is returning a value that is not catched?
It won't let me use %. It says "expression must have integral or enum type"
As for the other 2 questions, what do you mean? It won't work without returning a value as far as I can tell. Am I doing something redundant?
Last edited on
But your program seems to oexpect integers, so 'number' and 'multiple' should be int

You aren't assigning that returned value to anything. The program could work if the function were void multiplies();
And that's bad.
So should I change it to this:
factor = multiples(factor);
Last edited on
More like
1
2
3
4
if( multiples(number, multiple) )
	cout << multiple << " is a multiple of " << number << "." << endl;
else
	cout << multiple << " is not a multiple of " << number << "." << endl;
closed account (zvRX92yv)
if ( (number % multiple) == 0 )

Done.
Topic archived. No new replies allowed.