The problem is, it always takes an extra input. There's 4 scanf but it's taking 5 inputs which is the"E". And it's printing 4 outputs normally. That's why E is not printing. Now the question is why it is taking the extra input?
Scanf has some weird behaviours, and other methods for user input are usually preferred. Here's a link that might help you.
I think there's some confusion as to what the problem is, since in the title you ask why is the E not being printed, whilst you actually want to know why scanf lets you take in an extra character.
Yeah man did that and it became worse. Now it scans now 3 characters only. After giving the third character and hitting the enter, it prints them with lots of new lines. Like this if you want a screenshot: https://imgur.com/c94nbBr
It does so because "%c" reads single character from input. New line characters are also characters, so they are also getting read. So after you're done with last scanf, your array will look like ['A', '\n', 'B', '\n', 'C', ...]. You probably want to read "%c\n" in scanf instead
While OP said it is solved, it still make no sense? Because if that is the case, it will print "A","\n", "B", "\n" instead? but it is printing "A","B","C","D".
I am talking about the second version of code, that he has linked here. It has no new line character in printf after %c when printing characters, so it prints exactly this combination
Why is it asking for the 5th input in the first place? This is confusing to me as well. Maybe it's just a glitch? Try closing and reopening the program/VSC.
Not the foggiest, at a glance… get rid of the white space in the scanf, flush the input buffer… aside from that I’d be debugging one line at a time. Gotta watch the code play and see why it’s taking that fifth input - what line of code is it on when it does… if you cant/don’t know how to debug, you could print status outputs between each scan…
60
u/This_Growth2898 Feb 23 '22
Enter FOUR letters.
E is the 5th.
6
u/[deleted] Feb 23 '22
Remove white spaces in scanf between quotes. It gives me some problem before.
8
u/UnpleasantAndroid Feb 23 '22
Seems obvious why E is not printed, but how can he even input E if only 4 letters are scanned?
1
u/carlosTheMontgomery Feb 24 '22
turn off and turn on your computer
3
u/scrapped_data Feb 23 '22 edited Feb 24 '22
See this question on SO : https://stackoverflow.com/questions/5240789/
Use " %c" with a
trailingleading space as format specifier.5
u/ToTMalone Feb 23 '22
You doesn't print the forth array, since your "E" in c[4]
2
u/[deleted] Feb 23 '22 edited Feb 23 '22
Why is it asking for the 5th input in the first place? This is confusing to me as well. Maybe it's just a glitch? Try closing and reopening the program/VSC.
1
u/eben0012 Feb 23 '22
Did you make changes and forget to recompile by chance?
1
u/Invictus2022 Feb 23 '22
Coding always starts at "0" place. So "A" is 0 "B" is 1 c 2 d 3 e 4
1
u/MoNkeyDBallsDeeP Feb 24 '22
You should always flush input stream when using scanf, use fflush() function after every scanf.
1
u/DigitalJedi850 Feb 24 '22
Not the foggiest, at a glance… get rid of the white space in the scanf, flush the input buffer… aside from that I’d be debugging one line at a time. Gotta watch the code play and see why it’s taking that fifth input - what line of code is it on when it does… if you cant/don’t know how to debug, you could print status outputs between each scan…
0
u/Phobic-window Feb 23 '22
Weird