Sion Posted June 23, 2008 at 10:45 PM Report Share #193145 Posted June 23, 2008 at 10:45 PM Boas tou com um problema que embora pequena me faz uma cerca "comichão" :bored: Nesta funcao eu uso gets para ler uma string, o que acontece e que a uso duas vezes seguidas e por isso usei o while(getchar()!='\n'); para limpar o buffer , mas ao pedir o 1º nome ele obrigame a dar dois "enters" para seguir em frente :S. Nao existe maneira de solucionar isto?( ja tentei por o comando no inicio no fim e no meio da função o resultado é o mesmo xD char nomejog(char jogador[50]) { printf("\nInsira o seu nome\n"); // NOME DOS JOGADORES while(getchar()!='\n'); gets(jogador); } cumps _________________Cenas pá. Link to comment Share on other sites More sharing options...
Baderous Posted June 23, 2008 at 10:58 PM Report Share #193148 Posted June 23, 2008 at 10:58 PM Já que sabes de antemão qual o tamanho da string, porque não fazes: scanf("%50[^\n]",jogador); http://www.portugal-a-programar.pt/index.php?showtopic=10549 Link to comment Share on other sites More sharing options...
Rui Carlos Posted June 25, 2008 at 08:30 AM Report Share #193395 Posted June 25, 2008 at 08:30 AM Esse while deve aparecer sempre depois das leituras, e não antes. Antes também pode ser que funcione, mas já depende dos casos. E fazer o que o Baderous disse, ou usar o fgets, também é boa ideia. Rui Carlos Gonçalves Link to comment Share on other sites More sharing options...
capzulu Posted June 25, 2008 at 06:01 PM Report Share #193516 Posted June 25, 2008 at 06:01 PM Já que sabes de antemão qual o tamanho da string, porque não fazes: scanf("%50[^\n]",jogador); http://www.portugal-a-programar.pt/index.php?showtopic=10549 podes explicar essa linha d codigo? sff Link to comment Share on other sites More sharing options...
Baderous Posted June 25, 2008 at 06:09 PM Report Share #193519 Posted June 25, 2008 at 06:09 PM Abre o link. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now