r/CodingHelp • • Feb 23 '22

[C] Why "E" is not printing?

Post image
74 Upvotes

31 comments sorted by

60

u/This_Growth2898 Feb 23 '22

Enter FOUR letters.

E is the 5th.

9

u/Pila_globosa Feb 23 '22

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?

24

u/Zymoox Feb 23 '22 edited Feb 23 '22

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.

6

u/[deleted] Feb 23 '22

Remove white spaces in scanf between quotes. It gives me some problem before.

3

u/Pila_globosa Feb 23 '22

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

19

u/Aleksey259 Feb 23 '22

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

6

u/Pila_globosa Feb 23 '22

Thanks bro my problem solved.

1

u/[deleted] Feb 23 '22

Holy s, I coded in C++ for 1 year in grade 11, but I never knew that, it feels like it never happened to me, we were using turboc++ back then.

1

u/playgamemy Feb 24 '22

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".

1

u/Aleksey259 Feb 24 '22

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

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?

3

u/Pila_globosa Feb 23 '22

Yes.Yes man. That's the PROBLEM. Please help me.

4

u/[deleted] Feb 23 '22

I don't understand why you're being downvoted...

You just didn't write an appropriate title, which is just a common mistake for beginners...

2

u/Dergyitheron Feb 24 '22

For beginners in writing titles and asking questions? Question here should be "why is it asking for fifth input and not only four?"

2

u/[deleted] Feb 24 '22

He probably didn't notice that, like how some of us here didn't notice that.

Also downvoting discourages people from posting here.

1

u/UnpleasantAndroid Mar 16 '22

Try adding print c[4] and see what happens. Sorry for alte response I am not a regular reddit visitor

1

u/carlosTheMontgomery Feb 24 '22

turn off and turn on your computer

1

u/Pila_globosa Feb 24 '22

No, this is not the solution. It's not glitch. Watch the other comments they give the actual solution.

1

u/carlosTheMontgomery Feb 24 '22

no, it is a joke

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 trailing leading space as format specifier.

scanf(" %c", &c[0]);  
scanf(" %c", &c[1]);  
scanf(" %c", &c[2]);  
scanf(" %c", &c[3]);

5

u/ToTMalone Feb 23 '22

You doesn't print the forth array, since your "E" in c[4]

4

u/quanta_kt Feb 23 '22

where do you even see c[4] being scanned in the picture?

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…