Error reading, strings and integers

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 all the data, this way, 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 the reading of one more value, which I have no idea what it is.

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

What has been causing this? Why at the end of the test case does it ask for more?

Problem link .

    
asked by anonymous 03.09.2015 / 05:07

2 answers

2

This code does not make much sense so you can not know what you want. He has some problems and is poorly organized. I've been restructured so I can understand and I've solved the problem. I just do not know if he does what you want. The main problem was 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;
    scanf("%d", &j);
    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);
    }
     return 0;
}

See working on ideone .

If you want a solution with while you have in this other ideone .

    
03.09.2015 / 10:30
1

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

Note: if it is linux replace fflush(stdin); with __fpurge(stdin) ; if you search there are other ways to clear the buffer.

    
03.09.2015 / 06:06