Why my puts() output is getting displayed as blank? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my puts() output is getting displayed as blank?

https://code.sololearn.com/cklPDUOM0n8k/?ref=app

4th Jul 2021, 1:46 PM
Srinath
17 Answers
+ 3
in your code,after a single charecter input ,everything is read by gets() up to new line . So after a char input, put string in continuation. not in next line. hope it helps.
4th Jul 2021, 1:51 PM
Jayakrishna 🇮🇳
+ 2
i entered "hola" as input and got: h ola as output. Other than using gets(), what's wrong?
4th Jul 2021, 1:49 PM
Slick
Slick - avatar
+ 2
Verstappen, I don't know if it's true, I think scanf() has its own issues (cmiiw). This far I've seen people ran into problem when their program needs to read numbers & string in sequence (though not always). This time I found that reading char also has tricky parts : )
4th Jul 2021, 3:17 PM
Ipang
+ 1
Slick ohh , I mean i give the input like G (Gave enter key for new line) then IVE So my output became G and then blank, I think the enter sequence was taken as an input.
4th Jul 2021, 1:56 PM
Srinath
+ 1
Ahhh yeah yougotta treat the input as a stream, and as Jay explained
4th Jul 2021, 1:57 PM
Slick
Slick - avatar
+ 1
Verstappen, I mean add getchar(); // return value not assigned to anything Just a line before you read the string, using either gets() or fgets(). * It is highly recommended to NOT use gets()
4th Jul 2021, 2:16 PM
Ipang
+ 1
Replace gets(b) with fgets(b, 100, stdin);
4th Jul 2021, 2:47 PM
Solo
Solo - avatar
+ 1
Ipang That's brilliant! Works perfectly
4th Jul 2021, 3:02 PM
Srinath
+ 1
Ipang I just have a doubt, why don't I have a problem with newline (enter statement getting taken as input) in scanf and only here?
4th Jul 2021, 3:06 PM
Srinath
+ 1
Ipang yeah this one was tricky. Maybe I reckon it's because scanf gets all kinda data like int,float,char but gets() is only used to get char or string data so it treats every user input as a complete char including the enter sequence. I'm not sure tho its just my wild guess. Do correct me if Im wrong.
4th Jul 2021, 3:28 PM
Srinath
+ 1
The gets() function is used to read input as an ordered sequence of characters, also called a string. A string is stored in a char array.
4th Jul 2021, 5:10 PM
Solo
Solo - avatar
0
Slick yeah got it 👍
4th Jul 2021, 2:00 PM
Srinath
0
Verstappen, It could be, in that case, try to call getchar() without assigning the return value, just before reading the string. I tried to change gets() with fgets() and the string was still read as blank, until getchar() was placed in, prior to reading the string. It is however a good idea to NOT use gets() cause it's been known for being insecure.
4th Jul 2021, 2:02 PM
Ipang
0
Ipang what do you mean by calling getchar() without return value?
4th Jul 2021, 2:13 PM
Srinath
0
Vasiliy I just wanted to work with gets() thats why. Will use fgets() in future codes.
4th Jul 2021, 3:03 PM
Srinath
0
Hello Ehouman Jhon Elohim, Please, for your own safety and comfort, avoid exposure of personal information such as phone number in public areas such as this forum. It would be great if you could also remove your phone number in the comments you wrote in people's code, for the same reason. Regards,
6th Jul 2021, 1:41 PM
Ipang
- 2
I am Elohim Ehouman, I am of Ivorian nationality I am a beginner developer who needs your help to improve myself even more, friends I really need help and I would be grateful to you then the one where whoever wants to m 'Help me leave his WhatsApp number or telegram here is mine +2250779604730 thank you in advance
6th Jul 2021, 10:00 AM
Ehouman Jhon Elohim
Ehouman Jhon Elohim - avatar