can't figure this out

Pages: 12
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.

anyway:
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
// 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>

using namespace 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;
}
Last edited on
You're calculating the value of temp from the uninitialised garbage values of a and b. You need to input values and then do the calculation.
how do I go about that?
Move

temp = a % b;

to after you've inputted values for a and b.

Thanks
hello

your problem is you calculate the reminder before you asked for a and b values (line 15). change it to this it should work
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
// 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>

using namespace 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;
}


i transferred line 15 to line 20.
Nope, doesn't seem to do the trick.
any other suggestions?
I should, there must be some other error.
@hitmanben:
system( ) is evil: http://www.cplusplus.com/articles/j3wTURfi/
Last edited on

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
Last edited on
If you have more than on action/statement in an if, you need to use blocks { ... }.

Thus, add { after each if condition and } after the last statement that belongs to that if, so that it looks like this:
1
2
3
4
if (condition) {
  statement
  statement
}
Last edited on
Is there anything anyone else can see?? Or is there a way to get the remainder without %??
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.
Think he posted while we posted. Check the timestamps.
ah ok now its working.

Can anyone show me what this program would look like if I was to do it without using the % to get the remainder?
Or is there a way to get the remainder without %??

Yes there is. a%b is the same as:
1
2
3
4
5
int c;
while (a-b>0)
{
    c  = a-b;
}

Gaminics correct, I was a few seconds behind seeing your updated posts thanks

for a and b being int values,

remainder = a - ((a/b)*b)

This takes advantage of the fact that an int divided by an int will give an int answer.
1
2
3
4
5
int c;
while (a-b>0)
{
    c  = a-b;
}


a and b never change. This will loop forever.
Oops!!
What I meant was:
1
2
3
4
5
int c = a;
while (c-b>0)
{
    c  = a-b;
}
Pages: 12