### If statement multiplication by zero problem.

I have a minor problem.
I want to set an integer to zero when it easy equal to another integer, but it seems that the program for some reason won't set the integer to zero.
Here is the example of that code:

 ``123456789101112131415`` ``````#include using namespace std; int main () { int n = 2; int r = 2; if(n==r) n++; r*0; cout << " n is " << n << endl; cout << " r is " << r << endl; }``````

What am I doing wrong, it should say that "n is 3" and "r is 0".
`r*0;`

This does not modify r. You wouldn't expect `4*3` to modify the 4, would you?

The result of this multiplication must be captured somehow. Usually this is done with assignment:

`r = r*0;`

or you can shorten that with the multiplication+assignment operator:

`r *= 0;`

Or... since the goal is to assign a value of 0, just do that:

`r = 0;`
Thanks for the help. But I could swear that I've already tried those things, I really could.
Yep, yep I did.

that code was actually a test for the real thing, and it's still now working.

 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int main () { int n=2; int r=0; do{ if(n==r) n++; r = r*0; if (n<=10) cout<<"n je " << n << endl; if (r
Last edited on
Topic archived. No new replies allowed.