+ 5
Why gets () command is skipped when executing?
I tried to make an input to display the date of birth, name, address, and cellphone number. I use scanf () for the date input and gets () for the name and the like because I want when inputting the space name it will still be displayed as one line input, but when I execute it the program passes the name input and goes straight to the address input. why that thing could happen? and what is the solution? Please anyone, help me.. https://code.sololearn.com/c60CI92Jt7Wy/?ref=app
32 Respostas
+ 6
Don't use gets(). Forget about it.
https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used#:~:text=The%20function%20is%20unsafe%20because,NEVER%20USE%20IT!&text=You%20should%20not%20use%20gets,to%20stop%20a%20buffer%20overflow.&text=The%20correct%20thing%20to%20do,characters%20read%20from%20the%20user.
You can use a scanf for each input or an fgets to store everything at once.
+ 6
NotAPythonNinja omg you are taking about the same question of stack overflow I posted š
they explain exactly that, I didn't read it carefully.
I don't know why, but I considered the new line as a character worth more than a mere space.
All right I am glad of having learnt something new in this discussion.
Thank you š¤
+ 4
NotAPythonNinja Using getchar() to correct a new line left in the buffer by the notToUse gets(), when it exists scanf() to properly read the input.
+ 4
Thanks for helping everyone, i will edit my code tomorrow,time in my country its 22.45 now by the way, i have to go sleep, tomorrow is mondayš
+ 4
cs_s0uM the code is attached in the question
+ 3
I think your problem is just reading the input. Change all the gets with scanf and take care of the spaces and new lines left in the buffer.
If you inpit: NAME SURNAME
and read:
scanf("%s", name);
scanf("%s", surname);
Then the second scanf read the space.
You need to do do:
scanf("%s ", name);
With a space in "%s "
Since I don't know how you are giving inputs, I suggest you to add a space everywhere, even for the other inputs. Like scanf("%d ", number);
+ 3
NotAPythonNinja that's a bad suggesion
+ 3
Gede Yuda Aditya have you put a space in every scanf?
+ 3
Gede Yuda Aditya the Idea is to add a space in every place where you expect the user to enter a space or a newline
+ 3
Correct your program, so that we can tell where you are being wrong
+ 3
NotAPythonNinja in general the user press "enter" after typing the input, so scanf would not wait
However I read here:
https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string
That %d (and many other conversions) take care of a leading space.
So if you need to read: MY YEARS 13
with a scanf for each word/number you'd better use leading spaces and you don't need any for the number.
By using trailing spaces you would ignore the space between YEARS and 13 two times.
However I couldn't find any evidence of a possible hanging time due to the usage of a trailing space rather than a leading one. Please give evidence, I want to learn.
+ 3
NotAPythonNinja you can take off the leading spaces from " %d"
Check my previous comment
+ 3
NotAPythonNinja I got only the phone with Sololearn at the moment.
But I don't think scanf is waiting for a space. I think it may be that the first "enter" is discarded by the trailing space, so you would need a second one to end the output.
Anyway that's a good enough reason to prefer the leading space. Thank you for the evidence š¤š¤
+ 3
Gede Yuda Aditya just always put a space before %s like that " %s", that's it
+ 2
Davide
Thanks for the suggestion, i will try it.
+ 2
It's only because of how are you giving inputs :
scanf reads upto a space for word, upto a nondigit for Integer,.. gets read entire line upto a new line charecter.. So on using gets after %d by scanf then ex : 9 abc will read fine but ex:
9
abc
Here 9 read as int but \n will read by gets and abc will go to next gets().. So input taking into this consideration.. It works..
Hope it helps....
+ 2
NotAPythonNinja whoa, thanks
+ 1
NotAPythonNinja
I am remove that broken code at line 56 and 62, but still the gets(nama) input skiped.
+ 1
NotAPythonNinja
In Dev c++ sir.
+ 1
NotAPythonNinja
Owh really, so this problem my be from the editor ?