C program Getchar mistake

Chow Chun Yeung(Jimmy)
jimmy008002
Published in
2 min readSep 2, 2020

Today I am helping my tutor-student debug his program which is a 2048 game.

Here is program

And I get the result

The result show two loops , char and ‘Enter’ as an input.Getchar or scanf where the input type is char , there is some problem here. The reason is it also take line Feed(LF) when you press enter . The line Feed(LF) still in the input buffer .

Method

We can clean the input buffer

I add input=getchar(); in line47

here getchar get the line Feed

Result

--

--

Chow Chun Yeung(Jimmy)
jimmy008002
0 Followers
Editor for

I record everything i learn or some random pop up idea here for take note. I want to share all of it. Hope u love it.