Can someone take a peek at this code and tell me why the % operator is not returning the remainder like it should be. It compiles & runs but doesn't return the remainder properly it ALWAYS shows up as 0,also I don't know if my if statements are working properly since they both always show up. Again i think it has to do with the declaration of temp.
// This program is designed to take 2 numbers input, divide them, and return the answer
// If there is a remainder it will return the remainder, if there isn't it will return "no remainder"
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main(int argc, char *argv[])
{
int a;
int b;
int temp;
temp = a % b;
cout << "Enter two numbers seperated by a space you wish to divide." << endl;
cout << "Example (3 15) will return 3/15= and its remainder if there is one." << endl;
cout << endl;
cin >> a >> b;
// inputs int a space then int b
cout << endl;
if (temp == 0)
// if the remainder = 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "No Remainder" << endl;
if (temp != 0)
//if the remainder is greater then 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "Remainder= " << temp << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
// This program is designed to take 2 numbers input, divide them, and return the answer
// If there is a remainder it will return the remainder, if there isn't it will return "no remainder"
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main(int argc, char *argv[])
{
int a;
int b;
int temp;
cout << "Enter two numbers seperated by a space you wish to divide." << endl;
cout << "Example (3 15) will return 3/15= and its remainder if there is one." << endl;
cout << endl;
cin >> a >> b;
temp = a % b;
// inputs int a space then int b
cout << endl;
if (temp == 0)
// if the remainder = 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "No Remainder" << endl;
if (temp != 0)
//if the remainder is greater then 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "Remainder= " << temp << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Fix your if block braces. Indentation is not code in C++.
In this block of code, the lowest line always gets executed.
1 2 3 4
if (temp == 0)
// if the remainder = 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "No Remainder" << endl; // ALWAYS EXECUTED
In this block, they are wrapped in braces and both are part of the if block.
1 2 3 4 5 6
if (temp == 0)
{
// if the remainder = 0 output this set of statements
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "No Remainder" << endl;
}
As it is, the code posted by HitManBen does work (and demonstrates the problem with the if blocks). http://ideone.com/IUXP1
Seriously, we've fixed it. It now works. If you're not seeing it work, then you're not compiling the new code or your compiler is horribly horribly broken.