3
I declared
char pl1[10], pl2[10];
And I did this:
printf("Nome do Player1: ");
scanf("%10s", &pl1);
printf("Nome do Player2: ");
scanf("%10s", &pl2);
Thinking he was only going to read the first 10 characters the user typed. However, when I type a name with more than 10 characters the program reads wrong, example, is typed "dynamically", it associates "dynamicam" to pl1
and "nte" to pl2
. I tried too without the 10 on scanf
then it stores what exceeded the scope in the pl1
, example (using "dynamically") it ignores the first name typed (if the second is more than 10 characters), and assigns the first 10 characters typed when asking for the Nome do Player 2
á pl2
and the rest in pl1
. Case the typed in pl2
is less than 10 characters it stores dynamically in pl1
and the name that was entered in pl2
.
I think it would be better to change the vector size to 11 instead of decreasing the number of characters read
– hugomg
I couldn’t reproduce here, it was the same
– Leonardo
The situation I described is: https://ideone.com/OlzRmU
– pmg
Yes, this is the error that has been occurring with me, P1 has "dynamicam" P2 has the same "entity" in its example
– Leonardo
But if
p1
has space for 9 characters plus the terminator ... can not put there"dinamicamente"
!– pmg
Yes I know, but he should not assign the rest in the other vector, but just ignore the rest, because it is not possible to inform the P2 name, understand the problem now? Type, is to limit the heat the user can type
– Leonardo
To limit what the user can type while the program runs you will have to use functions specified to your Operating System. Or you use a library that makes it kind of easy, for example ncurses or pdcurses (in English).
– pmg
It’s working when I put it directly as the first thing in the program, when I put it inside the menu case or if it doesn’t work, it skips the P1 part
– Leonardo
Mingle
fgets()
withscanf()
is a bad idea. Apparently the menu usesscanf()
and leaves things hanging in the buffer. Change thesescanf()
forfgets()
followed bysscanf()
.– pmg
It disturbs the fgets() even being integer? I did not know rs I will try here
– Leonardo