It won't move. How is the loop wrong?

Hello

My setup, in theory, is to setup two arrays and within a infinite while loop pull a value randomly from each array and print the coordinates they make on screen at two second intervals. I want this to happen continuously but it does not here is the code:

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
38
39
40
41
42
43
44
#include <iostream>
#include <windows.h>

using namespace std;

void gotoxy (int x, int y);

int main()
{
int Xarray[10] ={2,8,14,20,26,32,32,44,50,56};
int Yarray[10] ={8,11,14,17,20,23,26,29,32,35};

while(true){

int i= rand() % 10;
int j= rand() % 10;


int rand1 = Xarray[i];
int rand2 = Yarray[j];

 for(i=rand1;;){
         for(j=rand2;;){
                                gotoxy(i,j);
                                cout << "+";
                                Sleep(2000);
                                gotoxy(i,j);
                                cout << " ";
     }
}

}cin.get();
Sleep(25);
system("pause");
return 0;
}

void gotoxy (int x, int y)
{
    COORD coord; // coordinates
    coord.X = x; coord.Y = y; // X and Y coordinates
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

I have empty for loops because I don't think I need to do anything in them. I am just trying to repeat the process of putting a coordinate on screen and taking it away randomly. Any help would be greatly appreciated.
i and j will never change ( both your fors loop forever )
How easily you cleared that up is beyound me. Deleating the For loops makes the target move around the screen so that section now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(true){
            
srand ( time(NULL) );

int i= rand() % 10;
int j= rand() % 10;

int rand1 = Xarray[i];
int rand2 = Yarray[j];

    gotoxy(rand1,rand2);
    cout << "+";
    Sleep(2000);
    gotoxy(rand1,rand2);
    cout << " ";
    
}cin.get();


Is there a way to eliminate the replication of coordinates once they have shown up once?
You'll need to store the values you already got and check if the new coordinated were already chosen.
The easiest way in your case may be a 2D 10x10 bool array, here is some pseudo-code:
1
2
3
4
5
6
7
8
9
array[10][10] = false
while ( true )
   i = rand ( 10 )
   j = rand ( 10 )
   if ( array[i][j] ) // already got those
      skip
   else // new coordinates
      array[i][j] = true // store in the array the fact that you got these coordinates
      do your stuff
If you want save some memory, you can use a vector of vector<bool> or a vector of bitsets
Last edited on
Topic archived. No new replies allowed.