How do I use a "or" no while in C

Asked

Viewed 66 times

0

I’m trying to make a naval battle application with the while stop with 2 conditions, which are the player’s mistakes or hits, where he can miss 5 times and have to hit all points, which in this case is 30.

        do{
    mostrarTabuleiro();
    tiro();
    system("pause");
    system("cls");
}while (erros<5||acertos<30);

I wanted to know if it’s some syntax error or if the error is before that part.

void tiro()
{
    int i,j,teste,cont;

    SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
    printf("\nVAMOS ATIRAAR!!\n");
    printf("Digite a linha: ");
    scanf("%d",&i);
    printf("Digite a coluna: ");
    scanf("%d",&j);
if (tabuleiro[i-1][j-1]!=-1)
{
    printf("Nao desperdice seu tempo!!\n");
    printf("Digite uma coordenada válida!\n");
}
else if (tabuleiro[i-1][j-1]==-1)
teste=navios[i-1][j-1];
acertos=0;
erros=0;
switch (teste)
        {
        case 1:
            erros=erros+1;
            tabuleiro[i-1][j-1]=teste;
            printf("VOCE ERROU\n");
            break;
        case 2:
            tabuleiro[i-1][j-1]=teste;
            acertos=acertos+1;
            printf("Voce acertou um Submarino (2 espaços)\n");
            break;
        case 3:
            tabuleiro[i-1][j-1]=teste;
            acertos=acertos+1;
            printf("voce acertou um contratorpedeiro (3 espaços)\n");
            break;
        case 4:
            tabuleiro[i-1][j-1]=teste;
            acertos=acertos+1;
            printf("voce acertou um navio-tanque (4 espaços)\n");
            break;
        case 5:
            tabuleiro[i-1][j-1]=teste;
            acertos=acertos+1;
            printf("voce acertou um porta-aviões (5 espaços)\n");
            break;
        }
}

Here is the shooting void, where this the stopping condition.

  • Your condition do_while is correct. Run the program with a Debugger to know the error line.

  • You could try to separate the operator, like this: while (errors < 5 || correct answers < 30), or even like this: while ((errors < 5) || (correct answers < 30))

  • There is no syntax error in the code, although little readable, unless the functions and variables involved do not exist. What error occurs? Where? needs more context, looking at just this can not know.

1 answer

-1


I think your problem is in the shooting method, this inside a "do while", whenever you enter the method and the user introduces a valid coordinate these to do:

else if (tabuleiro[i-1][j-1]==-1)
teste=navios[i-1][j-1];
acertos=0;
erros=0;
switch (teste)
  (...)

Each time the user enters the correct coordinates, you put the values back to 0 in the next iteration, so your variable hits and errors will always have the value 0 and 1, never higher than that which makes an infinite cycle.

For ease, you can replace the values:

acertos=acertos+1;
erros=erros+1;

For: successes++; errors++; That increments 1 the variable, if you want to increventar another value you can do: hits += x; errors += x; If x is a value of your choice, then += replaces:

variable = variable +

Browser other questions tagged

You are not signed in. Login or sign up in order to post.