:
This message was edited by BitByBit_Thor at 2005-7-13 9:59:27
:
This message was edited by BitByBit_Thor at 2005-7-13 9:58:26
: : : Hey,
: : :
: : : I have a (to me) strange problem with the _cgets() function. I have a simple word guessing game. The user can take a shot at guessing the word. The first time I call _cgets() everything works fine. Only the second time when I call it, it skips the statement and returns a zero string to my array. Why doesn't it work the second time? The third time it works again... the fourth time it doesn't. Do i need to clean up my string buffer in any way?
: : :
: : : I'll post part of the code:
: : :
: : : //Variable declaration (at the beginning of the sub)
: : : char guess[15] = { 12 }; //First byte contains max string len
: : : char* pGuess;
: : : ...
: : : //Later on I use this code to get input from the user
: : : _cputs( "\nGuess the word: " );
: : : guess[0] = wordLen; //Set max lenght of the guessed word
: : : pGuess = _cgets( guess );
: : :
: : : Like I said, the first time this works... the second it skips it (The values of guess: guess[0] = wordLen; guess[1] = 0; guess[2] = 0; guess[3] = 10)
: : :
: : : Thanks in advance (I'm still a bit of a noob when it comes to these things)
: : : Richard
: : :
: :
: :
: : I had the same problem with fgets() but I figured out that I needed to flush the stdin since it was still retaining junk. Try fflush(stdin); and see if that will fix your problem.
: :
: :
: :
:
: I tried to flush the input before using
:
: while (_kbbit() != 0) _getch();
:
:
: That didn't seem to work (it was almost as if the CrLf ("\n") sequence was still in the buffer and could not be 'flushed' by the code above).
:
: I figured then that fflush(stdin) would work, but even that does not :-S
:
: Should I try to 'clean' my buffer up, eg. fill guess[] with 0?
:
: Greets...
: Richard
:
Have you tried putting fflush(stdin) after each _cgets()? Thats where you should put it. Your guess array is not the problem since the junk like carriage return is still in the stdin buffer from the previous call to function _cgets(). Also wouldn't it be better to use fgets() anyways? Using fgets you can forgo the buffer length in your array since you specify it in the 2nd parameter.
_cgets(guess);
fflush(stdin);
or using fgets()
fgets(guess, sizeof(guess), stdin);
fflush(stdin);