Error in reading, strings and integers

Asked

Viewed 172 times

1

#include <stdio.h>

struct tetris{
        char nome[16];
        int pontuacao;
};

int main(){
    int i, nteste = 1, J, k, total = 0, maior, menor, pontos;
    while (scanf("%d\n", &J) && J != 0){
        struct tetris jogador[J];
        for (i = 0; i < J; i++){
            fgets(jogador[i].nome, 16, stdin);
            printf("LI NOME");
            total = 0;
            maior = 0;
            for (k = 0; k <= 11; k++){
                if (k == 11)
                    scanf("%d\n", &pontos);
                else
                    scanf("%d", &pontos);
                printf("LI PONTO");
                total += pontos;
                if (pontos > maior)
                    maior = pontos;
                else
                    if (pontos < menor)
                        menor = pontos;
                if (k == 0)
                    menor = maior;

            }
            jogador[i].pontuacao = total - maior - menor;
        }
        printf("ACABEI");
        for (k = 0; k < J; k++){
        printf("%s %d", jogador[k].nome, jogador[k].pontuacao);
        }
    }
    return 0;
}

After I type in all the data, like this, for example:

4
Zezinho
100 123 133 333 400 300 129 200 360 340 200 600
Luizinho
60 50 120 250 170 190 190 220 260 270 290 300
Carlinhos
10 10 20 10 10 10 10 20 20 20 20 20
Joaozinho
200 300 400 400 500 500 500 600 650 650 700 810

4 is equivalent to the number of players, the strings to their respective names and the integers to the scores. After typing them, my (incomplete) code should print the names and scores, but instead, it asks for reading one more value, which I have no idea what it is.

I’ve tried to use gets, scanf, and now the fgets - thinking that the error was in the reading of the strings - but it did not work.

What has been causing this? Why at the end of the test case he asks for an additional value?

Link to the problem.

  • Thanks for your time, buddy. while runs the program while the user does not type a J = 0, that is, when he type 0, the program terminates - it is a requirement of the site. The problem is not in the stopping condition of the readings, but in the amount of readings that the system is asking, always +1 of what it should. (Within each test case, at the end)

  • If I do this, the program will be incorrect, because the site says "0 <= J <= 1000 (J = 0 only to indicate final of the entry)"

  • Beauty. Just to try to clarify the problem: Instead of typing this: 2 Carlos 1 2 3 4 5 6 7 8 9 10 11 12 João 2 3 4 5 6 7 8 9 10 11 12 John 1 2 3 4 5 6 7 8 9 10 11 12 X Where X can be anything, an integer or a character.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

This code doesn’t make much sense so you can’t know what you want. It has some problems and is poorly organized. I was restructured to be able to understand and ended up solving the problem. I just don’t know if it does what you want. The main problem was in the while. As far as I understand it, it only exists to ensure that something above zero is typed, not to keep repeating everything:

#include <stdio.h>

struct tetris {
    char nome[16];
    int pontuacao;
};

int main() {
    int j = -1;
    while (scanf("%d", &j) && j != 0) { };
    struct tetris jogador[j];
    for (int i = 0; i < j; i++) {
        scanf("%15s", jogador[i].nome);
        printf("LI NOME\n");
        int total = 0;
        int maior = 0;
        int menor = 32767; //tem um risco aqui
        for (int k = 0; k <= 11; k++) {
            int pontos = 0;
            scanf("%d", &pontos);
            printf("LI PONTO\n");
            total += pontos;
            if (pontos > maior) maior = pontos;
            else if (pontos < menor) menor = pontos;
            if (k == 0) menor = maior;
        }
        printf("\n");
        jogador[i].pontuacao = total - maior - menor;
    }
    printf("ACABEI\n");
    for (int k = 0; k < j; k++) printf("%s %d\n", jogador[k].nome, jogador[k].pontuacao);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you want a solution with while has in this another ideone.

1

After the scanf you always put the function fflush(stdin); to clear the keyboard buffer, and within the scanf remove the \n who should print them should be the function printf("\n");

Note: if it is linux replace fflush(stdin); for __fpurge(stdin); if searching there are other ways to clean the buffer .

  • I did without using this and mine is working. This makes no sense and does not solve any problem.

Browser other questions tagged

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