+ 1
Why my puts() output is getting displayed as blank?
17 ответов
+ 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.
+ 2
i entered "hola" as input and got:
h
ola
as output. Other than using gets(), what's wrong?
+ 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 : )
+ 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.
+ 1
Ahhh yeah yougotta treat the input as a stream, and as Jay explained
+ 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()
+ 1
Replace gets(b) with fgets(b, 100, stdin);
+ 1
Ipang That's brilliant! Works perfectly
+ 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?
+ 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.
+ 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.
0
Slick yeah got it 👍
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.
0
Ipang what do you mean by calling getchar() without return value?
0
Vasiliy I just wanted to work with gets() thats why. Will use fgets() in future codes.
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,
- 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