This program essentially asks for a secret string, then asks a user to repeatedly guess single chars of that string until he guesses it all. It works however every second time the while loop is run it skips user input for the guessed char. How do I fix this?
int main(){
char guess;
char test2 [50];
char * s = test2;
char output [50];
char * t = output;
printf("Enter the secret string:\n");
fgets(test2, 50, stdin);
for (int i=0;i<49;i++){ //fills ouput with _ spaces
*(output +i)='_';
while(strcmp(s,t) != 0){
printf("Enter a guess:");
scanf("%c",&guess);
printf("You entered: %c\n", guess);
showGuess(guess,s, t ); // makes a string "output" with guesses in it
printf("%s\n",t);
}
printf("Well Done!");
}
Newline character entered in the previous iteration is being read by scanf. You can take in the '\n' by using the getc() as follows:
..
This changed worked for me. Though the right explanation and c leaner code is the one given by @7heo.tk
As pointed out in some other answers and comments, you need to "consume" the "newline character" in the input.
The reason for that is that the input from your keyboard to the program is buffered by your shell, and so, the program won't see anything until you actually tell your shell to "pass the content of its buffer to the program". At this point, the program will be able to read the data contained in the previous buffer, e.g. your input, followed by one the character(s) used to validate your input in the shell: the newline. If you don't "consume" the newline before you do another
scanf
, that secondscanf
will read the newline character, resulting in the "skippedscanf
" you've witnessed. To consume the extra character(s) from the input, the best way is to read them and discard what you read (what the code below does, notice theline after your
scanf
. What this line does is: "while the character read fromstdin
is not'\n'
, do nothing and loop.").As an alternative, you could tell your shell to not buffer the input, via the
termios(3)
functions, or you could use either of the curses/ncurses libraries for the I/O.So here is what you want:
Other comments on your code:
for
loop and never closed it. That might be causing problems.main
as a function returning an integer... So you should at leastreturn 0;
at the end.char * t = output;
copiesoutput
's value and usest
as a name for the new copy. This is wrong. You are indeed copying something, but you only copy the address (a.k.a reference) ofoutput
int
. As a result,output
andt
refer to the same data, and if you modifyoutput
,t
will get modified; and vice versa. Otherwise said, thoset
ands
variables are useless in the current state.fgets
for that.memset
function instead.'\0'
in last position.NULL
terminated after its meaningful content.Also, "_ spaces" are called "underscores" ;)
Here is a code that does what you want:
Feel free to ask questions in the comments, if you are not getting something. :)
Change
with
It should ignore
'\n'
.For a quick and dirty solution try
For a better solution redo your code to use
fgets()
and then parse the input.