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:

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
#include <iostream>
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
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.