Help with Project

Hi guys, I am fairly new to programming in general and i am taking a C++ course here at my local college. the teacher has asked us to do this

[Write an inline function bool is DivisibleBy(int m, int n). This function shall determine if m is divisible by n. Hint: The % operator fails if the right operand is 0. Make sure to handle this case appropriately]

For the life of me I cant get it right i get it to compile but it does not display nothing here is the code I have so far any help is welcome.

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
37
38
39
40
41

#include <cstdlib>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

/*
 * 
 */
inline int divisibleBy (int m, int n)
{
    if (n = 0)
    {
        cout << "sorry you cant divide by 0 please try again...." << endl;
    }
    else if (m % n == 0)
    {
        cout << "m is divisible by n thanks \n" << endl;
    }
    else
    {
        cout << "m is not divisible by n sorry try again \n" << endl;
    }
          
}

int main(int argc, char** argv) 
{

    cout << "Enter two numbers\n";
    int m, n;
    cin >> m >> n;
    divisibleBy(m, n);
    
    
    
    return 0;
}


Thanks Again
inline function bool is DivisibleBy(int m, int n)

The function is supposed to return true or false

In c/c++, to compare 2 values you use the '==' operator. The '=' is an assignment operator and will always return true when used for comparison
So how would i approach it? making it return false when the numbers cannot be divided? i have no idead how i would even code that sorry
Ok cool I worked on it i think i got it. Does this look correct??

Thanks

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
37
38
39
#include <cstdlib>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

/*
 * 
 */
inline int divisibleBy (int m, int n)
{
    if (n = 0)
    {
        cout << "sorry you cant divide by 0 please try again...." << endl;
    }
    else if (m % n == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
          
}

int main(int argc, char** argv) 
{

    cout << "Enter two numbers\n";
    int m, n;
    cin >> m >> n;
    cout << " 1 means it is divisible 0 means the numbers are not divisible" << divisibleBy;
    
    
    
    return 0;
}
closed account (28poGNh0)
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
# include <iostream>
using namespace std;

inline int divisibleBy (int m, int n)
{
    if (n == 0)
    {
        cout << "sorry you cant divide by 0 please try again...." << endl;
        return false;
    }
    else if (m % n == 0)
    {
        cout << "m is divisible by n thanks \n" << endl;
        return true;
    }
    else
    {
        cout << "m is not divisible by n sorry try again \n" << endl;
        return false;
    }
}

int main(int argc, char** argv)
{
    cout << "Enter two numbers\n";
    int m, n;
    cin >> m >> n;
    divisibleBy(m, n);

    return 0;
}
1
2
3
4
5
6
7
8
9
#include <iostream>
#define DivisibleBy(n, m)((m == 0) ? (cout << "Sorry you can't divide by ", 0) :((n % m) == 0))

using std::cout;

int main() {
	cout << DivisibleBy(81, 3) << '\n';
	return 0;
}
Oh makes so much sense now guys, thank you so much i feel a little dumb lol

Thanks Again

JCS
Topic archived. No new replies allowed.