Why gets () command is skipped when executing? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

15th Nov 2020, 12:32 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
32 Answers
+ 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.
15th Nov 2020, 12:49 PM
Davide
Davide - avatar
+ 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 🤗
15th Nov 2020, 3:15 PM
Davide
Davide - avatar
+ 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.
15th Nov 2020, 1:48 PM
Davide
Davide - avatar
+ 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😂
15th Nov 2020, 2:48 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
+ 4
cs_s0uM the code is attached in the question
15th Nov 2020, 6:25 PM
Davide
Davide - avatar
+ 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);
15th Nov 2020, 1:36 PM
Davide
Davide - avatar
+ 3
NotAPythonNinja that's a bad suggesion
15th Nov 2020, 1:44 PM
Davide
Davide - avatar
+ 3
Gede Yuda Aditya have you put a space in every scanf?
15th Nov 2020, 1:50 PM
Davide
Davide - avatar
+ 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
15th Nov 2020, 1:54 PM
Davide
Davide - avatar
+ 3
Correct your program, so that we can tell where you are being wrong
15th Nov 2020, 1:55 PM
Davide
Davide - avatar
+ 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.
15th Nov 2020, 2:40 PM
Davide
Davide - avatar
+ 3
NotAPythonNinja you can take off the leading spaces from " %d" Check my previous comment
15th Nov 2020, 2:51 PM
Davide
Davide - avatar
+ 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 🤗🤗
15th Nov 2020, 3:03 PM
Davide
Davide - avatar
+ 3
Gede Yuda Aditya just always put a space before %s like that " %s", that's it
16th Nov 2020, 10:15 AM
Davide
Davide - avatar
+ 2
Davide Thanks for the suggestion, i will try it.
15th Nov 2020, 1:40 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
+ 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....
15th Nov 2020, 2:11 PM
Jayakrishna 🇮🇳
+ 2
NotAPythonNinja whoa, thanks
15th Nov 2020, 2:49 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
+ 1
NotAPythonNinja I am remove that broken code at line 56 and 62, but still the gets(nama) input skiped.
15th Nov 2020, 1:16 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
+ 1
NotAPythonNinja In Dev c++ sir.
15th Nov 2020, 1:20 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar
+ 1
NotAPythonNinja Owh really, so this problem my be from the editor ?
15th Nov 2020, 1:26 PM
Gede Yuda Aditya
Gede Yuda Aditya - avatar