I don't know why the c++ bingo game while statement can't end

Asked 2 years ago, Updated 2 years ago, 85 views

When the user enters the (x, y) position on the 5x5 grid panel, 'O' is generated in that part, and I want to write a code that ends when it becomes a bingo.

Declare int check = 0; while When it becomes bingo with checkBingo function in the while statement, I want to make check into 1 and stop the loop when check becomes 1.

//
int check = 0;

int checkBingo(char a[5][5], int check) {
    for(int i =0; i<5;i++)
        if (a[i][1] == 'O' and a[i][2] == 'O' and a[i][3] == 'O' and a[i][4] == 'O' and a[i][0] == 'O') {
            cout <<"You won." <<endl;
            check = 1;
        }
    for (int j = 0; j < 5; j++)
        if (a[0][j] == 'O' and a[1][j] == 'O' and a[2][j] == 'O' and a[3][j] == 'O' and a[4][j] == 'O') {
            cout <<"You won." <<endl;
            check = 1;
        }
    if (a[0][0] == 'O' and a[1][1] == 'O' and a[2][2] == 'O' and a[3][3] == 'O' and a[4][4] == 'O') {
        cout <<"You won." <<endl;
        check = 1;
    }
    if (a[0][4] == 'O' and a[1][3] == 'O' and a[2][2] == 'O' and a[3][1] == 'O' and a[4][0] == 'O') {
        cout <<"You won." <<endl;
        check = 1;
    }
    return 0;

}

int main()
{
    char a[5][5] = {' '};
    cout << "---------------------";
    cout << endl;

    while (true) {
        print(a);
        checkBingo(a, check);
        if (check == 1)
            break;
        int x;
        int y;
        cout << "input : ";
        cin >> x >> y;

        a[x][y] = 'O';
        system("cls");
        cout << check;
    }
}

while-loop c

2022-09-20 22:14

1 Answers

If you look at the code you wrote, the value of check is changed in the checkBingo function,

This changes the value of check only within the checkBingo function, and when it comes out of the function, it retains its original zero value.

Rather, when the bingo is completed, set the return value of the checkBingo function to 1, and store the value in the check of the main function.

I've told you how to solve the problem right away, but if you think about it a little more, you'll be able to write it neatly.

I'm adding that it might be misleading because there's a little lack of explanation.

I explained it as if it was impossible to modify the global variable within the function, but it is not.

Since check was declared as a global variable, it is true that if you change the value in the above function, the changed value will be output when other functions call it.

However, the value does not change when executing the code you wrote because check was received as a parameter of the function.

In other words, within the scope of the checkBingo function, the variable check means the value received as a parameter, not as a global variable.

Therefore, even if you change the value in the checkBingo function, if you exit the function, you still retain the value declared by the existing global variable.

Therefore, if you delete the check that is delivered as a parameter, the code will be executed as you intended.

You can choose a convenient method, but I think it would be good to implement it in various ways when you study.


2022-09-20 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.