1

To solve my problem I need to get out of the while loop if the str2 is empty.

while (elementos <= tamanho - jogada || str2 == NULL);

This is the code:

#include <stdio.h>
#include <string.h>

#define BUFFER 100

unsigned int randaux() {
    static long seed = 1;
    return (((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff);
}

/* Implementação do procedimento MostraLamberta */

unsigned int MostraLamberta(int *tabuleiro, int tamanho) {
    int i, casa, elementos;
    int jogada = 0;
    char novostr[BUFFER], str2[BUFFER];    /* novo vetor para receber os char */
    do {
        scanf("%d %d", &casa, &elementos);
        for (i = 0; i < tamanho; i++) {    /*Loop para percorrer o int tabuleiro um a um */
            if (tabuleiro[i] == 0) {       /*Comparar se for 0 vai para o vetor como O */
                novostr[i] = 'O';
            } else {            /* Se não vai para o vetor como X */
                novostr[i] = 'X';
            }
        }
        if (tamanho >= 10) {
            novostr[i] = '\0';
            for (i = 1; i < tamanho; i++) {
                if (i == 10) {
                    printf("%d", (i) / 10);
                } else {
                    printf(" ");
                }
            }
            printf("\n");
            for (i = 1; i < tamanho + 1; i++) {
                if (i >= 10) {
                    printf("%d", i % 10);
                } else {
                    printf("%d", i);
                }
                printf;
            }
        } else {
            for (i = 0; i < tamanho; i++) {
                if (i == 10) {
                    printf("%d", i % 10);
                } else {
                    printf("%d", i + 1);
                }
            }
        }
        printf("\n");
        novostr[i] = '\0';    /*Termina a string */
        jogada++;
        printf("%s", novostr);
        printf("\nJogada [%d]: %d %d", jogada, casa, elementos);    /*Imprime nova string */
        for (i = casa - 1; i < elementos + casa - 1; i++) {
            if (tabuleiro[i] == 0) {    /*Comparar se for 0 vai para o vetor como O */
                novostr[i] = 'X';
                tabuleiro[i] = 1;
                continue;
            } else {            /* Se não vai para o vetor como X */
                novostr[i] = 'O';
                tabuleiro[i] = 0;
                str2[i] = tabuleiro[i];
            }
            novostr[i] = '\0';

            printf("%d", str2[i]);//in here if its empty, the loop is over
        }
        printf("\n");

    } while (elementos <= tamanho - jogada || str2 == NULL);

    if (jogada % 2 == 0) {
        printf("Jogada inválida, perde jogador 2.");
    } else {
        printf("Jogada inválida, perde jogador 1.");
    }
}

void main() {
    int i, num, saltos, tamanho, tabuleiro[BUFFER];    /*Implementa as variáveis inteiras */
    scanf("%d %d", &tamanho, &saltos);

    // saltar os primeiros números aleatórios, para ter sequências distintas
    for (i = 0; i < saltos; i++)    /*Loop */
        randaux();            /*Chama a funcão randaux */

    /* Chamar o procedimento para gerar um novo tabuleiro */
    for (i = 0; i < tamanho; i++) {
        num = randaux();        /*Grava um numero aleatório numa nova variável */
        if (num % 2 == 0) {     /*VC* se o numero aleatório C) par ou impar */
            tabuleiro[i] = 0;
        } else {
            tabuleiro[i] = 1;
        }
    }

    MostraLamberta(tabuleiro, tamanho);    /*Chama a função e passa dois argumentos) */
}

[my output]

[expected output]

2
  • What is "the problem" ? Commented Nov 20, 2022 at 15:06
  • Hi, sorry for my english. Like the output the loops dont break if the str2 array its empty. printf("%d", str2[i]);//in here if its empty, the loop is over Commented Nov 20, 2022 at 15:21

2 Answers 2

1

It is unclear what your code does, but there are some problems:

  • the do / while loop is an error prone construction. It is less confusing to use a for (;;) (aka for ever) loop and make explicit tests to break from it, such as if (scanf("%d %d", &casa, &elementos) != 2) break;

  • the second part of the test while (elementos <= tamanho - jogada || str2 == NULL); is moot: str2 is an array, it cannot be NULL. If you want to test for an empty string, use str2[0] == '\0' instead.

  • str2 is not properly constructed: the only place where it gets modified is: str2[i] = tabuleiro[i]; but tabuleiro[i] is set to 0 just before this statement, so part of str2 is uninitialized and the only elements that get modified are null bytes.

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

Comments

0

In the OP code, the following lines get into infinite loop.

while (elementos <= tamanho - jogada || str2 == NULL);

Few potential issues to look at:

  • The 'str2' expression is always NON-null. Recall that in "C", the value of an array is the address of the first element. If you want to test is a string is "empty" consider strlen(str2) == 0, or similar.
  • For any case that str2 is "not empty", the while will go into infinite loop if elementos <= tamanho - jogada. Usually, you will see modification to one of the variables in the condition inside the loop body. e.g. jopada++

Comments

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.