function issue

I can't figure out what I'm doing wrong here, this program compiles and runs but ALWAYS returns the remainder as "1" even if its not. My goal here was to use a seperate function for remainder and not use the % operator to find it. Can anyone look this over and tell me what I'm doing wrong?


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 <iostream>
#include <cstdlib>
#include <cstdlib>

using namespace std;

int remainder(int a, int b);

int main(int argc, char *argv[])
{
    int a;
    int b;
    
    cout << "Enter two numbers, seperated by a space, you wish to divide." << endl; // prompt line
    cout << "Example: (3 15) will return 3/15= and its remainder if there is one!" << endl; // prompt line
    cout << endl; // adds blank space
	cin >> a >> b; // input statement
	
        if (remainder == 0) // if the remainder == 0 (does = 0) output the following set of statements
    {  
       	cout << a << " / " << b << " = " << ( a / b ) << endl; // prints the equation and solves it
       	cout << "No Remainder" << endl; // returns no remainder
    }
        if (remainder != 0) // if the remainder !=0 (does not = 0) output the following set of statements
    { 
        cout << a << " / " << b << " = " << ( a / b ) << endl; // prints the equation and solves it
        cout << "Remainder= " << remainder << endl; // prints the remainder from the above equation   
    }
	
	system("PAUSE"); // pause before exit
	return EXIT_SUCCESS; // hit any key to exit
}

int remainder(int a, int b)
{   
    int remainder;
    
    remainder = a - ((a/b)*b); // the following statement allows "c" to become the remainder without using the operator %
}
You're not returning anything in remainder(). The compiler should at least show a warning about that.
Really, that code compiles? Get a new IDE!

a) Your remainder() function is lacking a return statement.

b) You're using 'remainder' as if it were a variable, not a function, on lines 19, 24 and 27. There IS no variable 'remainder', because it's only defined inside the scope of remainder().

Those are small mistakes, but definitely something your compiler should catch and cry about...
It didn't, then again I'm using devC++ which hasn't been updated in forever,

How would I go about fixing this code so it works properly?

and what should I be returning?

 
return 0;

???
the warning I'm getting is "[warning] the address of 'int remainder(int, int)', will always evaluate as 'true'"
I guess I'm not understanding how to use functions correctly. What can I do to fix this?
If you can, replace your IDE as soon as possible. I've never used it myself, but I've seen some of the more experienced people in here mock it to bits.

For your code, just think about it: You've written a function to perform a certain calculation, which works perfectly [or should, as far as I can tell]. The problem is that there is no communication between the function and the code that calls it.

First, your function isn't returning a value. Think for a second: the calling code (here: the main()) can't see what happens inside remainder(). What do you want remainder() to tell main()?

Secondly, even if remainder() was "talking", nobody would be listening. To use a value calculated by a function, it has to be saved somewhere. Thus, you'll need a variable to store the returned value inside main(). You can call it 'remainder' if you wish, so you don't have to edit much code.
Thanks for the reading material, I'm understanding a little better, I'm returning

 
return (remainder);


now at the end of the function, is this correct?

I want remainder to tell main the result of my equation being int remainder; So how do I get main to pull this information?
It's correct, although the braces are unnecessary - it should be just return remainder;

So how do I get main to pull this information?

The function call becomes the returned value itself, as shown in the tutorial.
You are not calling your function:
 
        if (remainder == 0) // if the remainder == 0 (does = 0) output the following set of statements 

What that does is examine the function pointer called 'remainder'. That is it has the value of your function's address in memory. That will always be 'true'.

To actually call your function you need to pass it some parameters like this:
 
        if (remainder(a, b) == 0) // if the remainder == 0 (does = 0) output the following set of statements 

Last edited on
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
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
#include <cstdlib>

using namespace std;

int remainder(int a, int b);

int main(int argc, char *argv[])
{
    int a; // declaration of integer a
    int b; // declaration of integer b
    
    cout << "Hello, my name is Cal Que Later, I'll help you divide!" << endl; // prompt line
    cout << "Enter two numbers, seperated by a space, you wish to divide." << endl; // prompt line
    cout << "Example: (3 15) will return 3/15= and its remainder if there is one!" << endl; // prompt line
    cout << endl; // adds blank space
	cin >> a >> b; // input statement
	cout << endl; // adds blank space
	
        if (remainder(a, b) == 0) // if the remainder == 0 (does = 0) output the following set of statements
    {  
       	cout << a << " / " << b << " = " << ( a / b ) << endl; // prints the equation and solves it
       	cout << "No Remainder" << endl; // returns no remainder
    }
        if (remainder(a, b) != 0) // if the remainder !=0 (does not = 0) output the following set of statements
    { 
        cout << a << " / " << b << " = " << ( a / b ) << endl; // prints the equation and solves it
        cout << "Remainder= " << remainder(a, b) << endl; // prints the remainder from the above equation   
    }
    
    cout << endl; // adds blank space
    cout << "Check my math with the equation below!!" << endl; // prompt line
    cout << endl; // adds blank space
    cout << ( a / b ) << " * " << b << " + the remainder of " << remainder(a, b) << " = " << ( a / b ) * b + remainder(a, b) << endl; //check your work statement
    cout << endl; // adds blank space
    system("PAUSE"); // pause before exit
	return EXIT_SUCCESS; // hit any key to exit
}

int remainder(int a, int b) //function remainder
{   
    int remainder; //declaration of integer remainder 
    
    remainder = a - ((a/b)*b); // the following statement allows "c" to become the remainder without using the operator %
    
    return remainder; //return value of int remainder to main
}


here's the final product, it works perfect and I understand much better, thanks for the extra push..
Topic archived. No new replies allowed.