C: Use two gets () followed (player names)?

0

I'm trying to play a game, for this I have displayed a menu and then I used the command switch to access the options, if the user wants to play it select 1 and will be asked the name of the players 1 and 2, for this I used get but at the time I run the code it skips player 1 and goes directly to player 2, how do I solve it? I'm using it like this:

printf("Nome do Player1: ");
gets(player);
printf("\n");
printf("Nome do Player2: ");
gets(pl2); 

NOTE: I will correctly declare the vectors, and these codes are within case 1 of switch

    
asked by anonymous 12.06.2015 / 05:45

2 answers

1

Do not use get it is not very secure and enough problem with buffer , a good alternative is scanf .

Example:

printf("Nome do Player1: ");
scanf("%s", &player);
printf("\n");
printf("Nome do Player2: ");
scanf("%s", &pl2);

If you want, fgets looks at here .

    
12.06.2015 / 05:55
1

I would be even more reticent about the indiscriminate use of scanf . In particular, this same scanf("%s",&p) may be a target of buffer overflow . It would be important to limit the length of the received string: scanf("%100s",&p) .

(Actually, I'm even smarter about scanf ; I prefer to create my own versions using getc and putc .)

    
02.05.2017 / 18:52