0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>

typedef struct jogador
{
    char nome[30];
    int idade;
    int chutes;
    int gols;
}Jogador;

int main()
{
    Jogador j1, j2;

    int r1, r2;

    gets(j1.nome);
    scanf("%d", &j1.idade);
    scanf("%d", &j1.chutes);
    scanf("%d", &j1.gols);
    printf("Jogador 1 Terminou");

    gets(j2.nome);
    scanf("%d", &j2.idade);
    scanf("%d", &j2.chutes);
    scanf("%d", &j2.gols);
    printf("Jogador 2 Terminou");

    r1 = (j1.gols * 100)/j1.chutes;
    r2 = (j2.gols * 100)/j2.chutes;

    if(r1 > r2)
    {
        printf("%s (%d)", j1.nome, j1.idade);
    }
    else printf("%s (%d)", j2.nome, j2.idade);
    printf("Fim de Programa");
}

After the second gets (gets(j2.nome);) read, my program does not scan the others values and just stop. Ive, tried using fgets but end up with the same problem.

If any one could help i appreciate.

  • To begin with, you should **never** use `gets()`. Here is why, https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used. – alex01011 Mar 04 '21 at 21:18
  • Call `getchar()` right after the second `gets()` call,consume the newline left in the buffer. – alex01011 Mar 04 '21 at 21:22
  • i was able to solve after using a getchar before the second fgets. thanks for the help – yuri fernandes Mar 04 '21 at 21:38
  • although, the print result is skipping a line after j1.nome and j2.nome – yuri fernandes Mar 04 '21 at 21:42
  • `gets` stores the newline. So it will include that newline when you print it. Strip the trailing newline if you don't want it. – kaylum Mar 04 '21 at 21:47

0 Answers0