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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
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.

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
#include<iostream>
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<n)
        r++;

        if (r<=n)
        cout << "r je " << r <<endl;

}while(n<10 || r<10);
}
Last edited on
Topic archived. No new replies allowed.