switch statement and for loop problem

hi!
i'm trying to make a memory game
but the part of the code that should tell the program where (ex:red and the another red is) is not working



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int i = (rand()%2)+1; // generating random number
switch(i)   // part where i suspect the problem to be
{
    case 1:  
    for (int a = 0; a < 6 ; )
    {
        int g = 6;


        int x = 0;

    x++;
    ColorPlace[a] = x; // where the first RED or blue is going to be



    ColorPlace2[a] = g; // where the second RED or BLUE is going to be
    g++;
    cout << a << endl; // i made some testing that the loop is running...
       a++;                          // ..and it was!
    
    }
    for (int a = 0;a < 6;)  // some more testing 
    {
     // the problem is that the numbers for where (ex: RED or Blue) should be..
     // dont change they are always the same
          
    cout << ColorPlace[a] << endl;
    cout << ColorPlace2[a] << endl;
    Sleep(1500);
    a++;
    }

    break;

    case 2: // this part of code is for now the same it will be changed

    for (int a = 0; a < 6 ; )
    {

        int h = 6;
        int y = 0;


    y++;
    ColorPlace[a] = y;


    ColorPlace2[a] = h;
    h++;
    a++;
     cout << a << endl;
    }
    for (int a = 0;a < 6;) // and yet again testing the code 
    {


    cout << ColorPlace[a] << endl;
    cout << ColorPlace2[a] << endl;
    Sleep(1500);
    a++;
    }
    break;
}

by testing different parts of the code I have pinpointed the problem to be
the for loop and switch statement

THE PROBLEM:
the problem is that the numbers for where (ex: RED or Blue) should be
dont change they are always the same
What am i doing wrong?
thank you!
Last edited on
Let consider the code snip.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   for (int a = 0; a < 6 ; )
    {
        int g = 6;


        int x = 0;

    x++;
    ColorPlace[a] = x; // where the first RED or blue is going to be



    ColorPlace2[a] = g; // where the second RED or BLUE is going to be
    g++;
    cout << a << endl; // i made some testing that the loop is running...
       a++;                          // ..and it was!
    
    }


The problem is that your variables 'x' and 'g' are local variables of the for body. So for each iteration they are set correspondingly to 0 and 6. So there is no any sense in statements

x++;
g++;


You could change your code the following way

1
2
3
4
5
6
7
8
9
10
   for (int a = 0, g = 6, x = 1; a < 6 ; a++, g++, x++ )
    {
    ColorPlace[a] = x; // where the first RED or blue is going to be



    ColorPlace2[a] = g; // where the second RED or BLUE is going to be
    cout << a << endl; // i made some testing that the loop is running...
                                  // ..and it was!
    }

Last edited on
thank you
that was a pretty simple error that i made, but thank you once again
Topic archived. No new replies allowed.