0

Ok, I'm sorry in advance, I know there is a thousand examples of how do-while loops work and how to exit them. I swear I've tried them all. For the life of me, I cannot get this to exit when the user enters a 0. There has to be something I'm over-looking. How I got the one to work inside the else if loop but the main one is beyond me. I would appreciate any help or direction.

int main()
{
    char connectBoard[6][7];
    int i , j;
    char c;
    int turn = 1;
    char player1 = 'x';
    char player2 = 'o';
    int spot;

    for( i = 0; i < 6; i++) //sets up 2D array board
    {
        for( j = 0; j < 7; j++)
        {
            c = '.';
            connectBoard[i][j] = c;
        }
    }
    for( i = 0; i < 6; i++)// prints out 2D array board
    {
        for( j = 0; j < 7; j++)
        {
            printf("%c", connectBoard[i][j]);
        }
        printf("\n");
    }
    printf("=======\n");
    printf("1234567\n");

    do{
        if( turn%2 != 0 ) //player1 turn
        {
            for( i = 5; i < 6; i++)
            {
                for(j = 0; j < 1; j++)
                {
                    printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player1);
                    scanf("%d", &spot);
                    j = spot - 1;
                    if( connectBoard[i][j] == '.' )
                    {
                        connectBoard[i][j] = 'x';
                    }
                    else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
                    {
                        do
                        {
                            i--;
                        }while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
                        connectBoard[i][j] = 'x';
                        if( i == -1 )
                        {
                            printf("***Bad entry, try again: ");
                            scanf("%d", &spot);
                            j = spot - 1;
                            i = 5;
                            connectBoard[i][j] = 'x';
                        }
                    }
                    turn++;
                }
                break;
            }
            printf("\n");
            for( i = 0; i < 6; i++)// prints out 2D array board
            {
                for( j = 0; j < 7; j++)
                {
                    printf("%c", connectBoard[i][j]);
                }
                printf("\n");
            }
            printf("=======\n");
            printf("1234567\n");
        }
        if( turn%2 == 0 ) //player2 turn
        {
            for( i = 5; i < 6; i++)
            {
                for(j = 0; j < 1; j++)
                {
                    printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
                    scanf("%d", &spot);
                    j = spot - 1;
                    if( connectBoard[i][j] == '.' )
                    {
                        connectBoard[i][j] = 'o';
                    }
                    else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
                    {
                        do
                        {
                            i--;
                        }while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
                        connectBoard[i][j] = 'o';
                        if( i == -1)
                        {
                            printf("***Bad entry, try again: ");
                            scanf("%d", &spot);
                            j = spot - 1;
                            i = 5;
                            connectBoard[i][j] = 'o';
                        }
                    }
                    turn++;
                }
                break;
            }
            printf("\n");
            for( i = 0; i < 6; i++)// prints out 2D array board
            {
                for( j = 0; j < 7; j++)
                {
                    printf("%c", connectBoard[i][j]);
                }
                printf("\n");
            }
            printf("=======\n");
            printf("1234567\n");
        }

    }while( spot != 0 );




    return 0;
}
6
  • Why do while? Use a simple While in a function an do a return or break. In my opinion do while should be avoid as much as possible. And please stop using printf like this, this is awfull, buffering etc.... read printf man :) Commented Sep 30, 2016 at 9:23
  • j = spot - 1; when spot is 0 address the matrix out of bound, so UB. Commented Sep 30, 2016 at 9:24
  • BTW the printf that ask for 0 to exit is inside a double for loop. You must exits both to kill the external while loop. Commented Sep 30, 2016 at 9:25
  • This code is needlessly complex. You should break it down into functions. As a side-effect, that will probably cause most problems to go away. Commented Sep 30, 2016 at 9:40
  • The do-while loop is hard to follow since it contains so many statements. Define a function for "player turn" and call it for each player. That way you also avoid code duplication. Commented Sep 30, 2016 at 9:40

3 Answers 3

1

After reading the spot variable, you can check if the value is 0 and break:

   printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
   scanf("%d", &spot);
   if (spot == 0)
     return 1;
   j = spot - 1;

and change your do while to a while loop to avoid confusion.

Sign up to request clarification or add additional context in comments.

1 Comment

ah yes, in that case we can exit.
0

Use a variable only for that loop. You are using spot inside your loop to do other things, use it only for the while purpose.

Then, you know the diference between do/while and while? I dont understand very well your program, but if you are reading a spot, you can read it at beggining and outside the loop and then check it with a while loop. Its the same but maybe its more easy for you.

2 Comments

ok, so I see what you mean by using a different variable for the while loop, but then how would the user choose 0 to quit? If I'm understanding right, I think I may have tried that already. I made another variable called end, then set it to equal whatever the user input for spot, and used just a while loop to see if end was equal to 0, which also didn't work for me. Its a program to play connect 4.
Then why if the player enter 0 you dont turn end into the while exit condition?
0

Why are you using spot for while loop. as per C language, It stores a garbage value.

Even if you use, please do not set values to it. Please check basic C variables declaration.

1 Comment

Mainly right, but in this specific case the loop is a do-while, so spot is inited by internal loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.