### simple problem but confused.

Hey, I'm trying something to have a number change its value based on another number in an attempt to get a pattern going so im starting with one value constantly changing back and forth from one digit to another in real time so I tried doing this:

 ``1234567891011121314151617181920212223242526272829`` ``````#include using namespace std; int main (){ int r = 2; int *s; int t = 5; int *u; *s = r ; *u = t; if(r == *s){ r = t; }else {cout << " :( " << endl; } if (r = t){ r = *s; }else{cout << ">:{" << endl;} cout << r << endl; return 0; }``````

Im confused how It landed on R's original value, shouldn't it loop back and forth

An alternative I tried is

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main (){ int r = 2; int *s; int t = 5; int *u; *s = r ; *u = t; do{r = t;}while(r == *s); do{r = *s;}while(r = t); cout << r << endl; return 0; }``````

Any Advice, is it my IDE?
Not really sure what you're trying to do, and no idea why you're using pointers here, but:

`(r = t)`
Is not a comparison, it's an assignment. Change this to `r == t`
if (r = t) is basically r = t; if (r)

secondly, you are dereferencing a dangling pointer, so this is illegal:

int *s; // doesn't point to anywher!
*s = r; // dereference a random pointer value

Thanks I'm trying to set a value to one digit thatll trigger an if statement thatll trigger the other so it would go into an endless pattern
And I r = t is a typo I caught afterwards.
Last edited on
It seems to me like you don't know how program flow works.
Why not just use loops?
Topic archived. No new replies allowed.