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?
#include <iostream>
#include <cstdlib>
#include <cstdlib>
usingnamespace 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 %
}
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...
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.
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
#include <iostream>
#include <cstdlib>
#include <cstdlib>
usingnamespace 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..