C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
_cgets() Problem Posted by BitByBit_Thor on 11 Jul 2005 at 7:12 AM
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
Report
Re: _cgets() Problem Posted by shaolin007 on 11 Jul 2005 at 8:07 AM
: 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.


Report
Re: _cgets() Problem Posted by BitByBit_Thor on 13 Jul 2005 at 9:58 AM
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





Report
Re: _cgets() Problem Posted by shaolin007 on 13 Jul 2005 at 11:48 AM
: 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);

Report
Re: _cgets() Problem Posted by BitByBit_Thor on 14 Jul 2005 at 3:19 AM
:
: 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);
: 

:

Actually... I tried it before and after the _cgets() call, just to be sure.

I messed around with it a bit and it seems that my buffer always needs to be large enough to hold the string lenght, plus the "\n" characters...
I'll try to explain: the word is 5 letters long. So then I would set the max string to 7 so the "\n" sequence can be read also. However, this fails ofcourse if the user decides to input a 6 letter word :-S
And for some reason, neither my flush code, nor the fflush(stdin) seem to have any effect...

Greets...
Richard

Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 5:51 AM
: :
: : 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);
: : 

: :
:
: Actually... I tried it before and after the _cgets() call, just to be sure.
:
: I messed around with it a bit and it seems that my buffer always needs to be large enough to hold the string lenght, plus the "\n" characters...
: I'll try to explain: the word is 5 letters long. So then I would set the max string to 7 so the "\n" sequence can be read also. However, this fails ofcourse if the user decides to input a 6 letter word :-S
: And for some reason, neither my flush code, nor the fflush(stdin) seem to have any effect...
:
: Greets...
: Richard
:

According to MSDN if you had a 5 letter string then the size of the array would have to be at least 8. guess[0] would be the max size of the array. guess[1] would be the actual size after cr\lf and the last element would be '\0'.



Report
Re: _cgets() Problem Posted by stober on 14 Jul 2005 at 10:36 AM
: :
:
: According to MSDN if you had a 5 letter string then the size of the array would have to be at least 8. guess[0] would be the max size of the array. guess[1] would be the actual size after cr\lf and the last element would be '\0'.
:

:
:
:

Where did you read that? c-style character arrays do not contain the length of the string nor the number of bytes allocated to it. using fgets() the length of the string would have to be at least 7 (5 characters, plush '\n' and '\0).
char guess[7];
fgets(guess,sizeof(guess),stdin);


So if I typed in "Hello" <Enter>, guess could contain this:
guess[0] = 'H'
guess[1] = 'e'
guess[2] = 'l'
guess[3] = 'l'
guess[4] = 'o'
guess[5] = '\n'
guess[6] = 0


using fflush(stdin) normally has undefined behavior -- that is to say the C standards do not define what happens when you attempt to flush stdin. It may work the way you want with some compilers but may not work at all (or trash your ocmputer) with others.
Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 11:26 AM
This message was edited by shaolin007 at 2005-7-14 11:45:33

Where did you read that?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT__cgets.asp

: using fflush(stdin) normally has undefined behavior -- that is to say the C standards do not define what happens when you attempt to flush stdin. It may work the way you want with some compilers but may not work at all (or trash your ocmputer) with others.
:

I guess that probably is true since I was using the file msvcrt.dll. I figured when I was having the problem with fgets(,,stdin) in my program it was corrected using fflush(stdin). I figured that it cleared the junk out of the stdin buffer which is defined by _iob[0]structure.



edit


I looked up fflush() on MSDN and it recommends flushing the (stdin) before using a stdin function because the previous stdin function might of overran the buffer. fflush() will clear the contents of this buffer.

example code from MSDN
Example
// crt_fflush.c
#include <stdio.h>
#include <conio.h>

int main( void )
{
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );      
      // Security caution!
      // Beware allowing user to enter data directly into a buffer
      // without checking for buffer overrun possiblity.
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );   // fflush on input stream is an extension to the C standard
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s\n", string );
}




Report
Re: _cgets() Problem Posted by stober on 14 Jul 2005 at 11:53 AM
This message was edited by stober at 2005-7-14 11:54:8

:
: I looked up fflush() on MSDN and it recommends flushing the (stdin) before using a stdin function because the previous stdin function might of overran the buffer. fflush() will clear the contents of this buffer.

:

Microsoft does not set the ANSI standards. Yes, their compiler allows fflush(stdin), but most others do not. And Microsoft compilers are notorious for not being ANSI compliant in many other areas too.


Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 12:10 PM
: This message was edited by stober at 2005-7-14 11:54:8

: :
: : I looked up fflush() on MSDN and it recommends flushing the (stdin) before using a stdin function because the previous stdin function might of overran the buffer. fflush() will clear the contents of this buffer.

: :
:
: Microsoft does not set the ANSI standards. Yes, their compiler allows fflush(stdin), but most others do not. And Microsoft compilers are notorious for not being ANSI compliant in many other areas too.
:
:
:



The compilers might not but I don't think that would matter since the stdin is handled by the OS right? What does the standards say about it? I'm curious to know.


Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 12:40 PM
: : This message was edited by stober at 2005-7-14 11:54:8

: : :
: : : I looked up fflush() on MSDN and it recommends flushing the (stdin) before using a stdin function because the previous stdin function might of overran the buffer. fflush() will clear the contents of this buffer.

: : :
: :
: : Microsoft does not set the ANSI standards. Yes, their compiler allows fflush(stdin), but most others do not. And Microsoft compilers are notorious for not being ANSI compliant in many other areas too.
: :
: :
: :
:
:
:
: The compilers might not but I don't think that would matter since the stdin is handled by the OS right? What does the standards say about it? I'm curious to know.
:


edit


I found the standard on fflush(). It doesn't say anything about stdin streams but it does mention output streams.



7.19.5.2 The fflush function
Synopsis
1 #include <stdio.h>
int fflush(FILE *stream);
7.19.5.2 Library 269&#3338;ISO/IEC 9899:1999 (E) ISO/IEC
Description
2 If stream points to an output stream or an update stream in which the most recent
operation was not input, the fflush function causes any unwritten data for that stream
to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.
3 If stream is a null pointer, the fflush function performs this flushing action on all
streams for which the behavior is defined above.
Returns
4 The fflush function sets the error indicator for the stream and returns EOF if a write
error occurs, otherwise it returns zero.

Report
Re: _cgets() Problem Posted by stober on 14 Jul 2005 at 12:43 PM
This message was edited by stober at 2005-7-14 12:45:36

:
: I found the standard on fflush(). It doesn't say anything about stdin streams but it does mention output streams.
:

:
:
: 7.19.5.2 The fflush function
: Synopsis
: 1 #include <stdio.h>
: int fflush(FILE *stream);
: 7.19.5.2 Library 269&#3338;ISO/IEC 9899:1999 (E) ISO/IEC
: Description
: 2 If stream points to an output stream or an update stream in which the most recent
: operation was not input, the fflush function causes any unwritten data for that stream
: to be delivered to the host environment to be written to the file; otherwise, the behavior is
: undefined.
: 3 If stream is a null pointer, the fflush function performs this flushing action on all
: streams for which the behavior is defined above.
: Returns
: 4 The fflush function sets the error indicator for the stream and returns EOF if a write
: error occurs, otherwise it returns zero.
:

:

otherwise, the behavior is undefined.
that's whay fflush(stdin) has undefined behavior.


Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 12:57 PM
This message was edited by shaolin007 at 2005-7-14 13:0:48

otherwise, the behavior is undefined.
that's whay fflush(stdin) has undefined behavior.



Ahh, I misread that part. I thought they were still talking about output streams. Anyways, I guess it works with the MS msvcrt.dll Windows OS and nothing else. One thing though, why would MS in the example on MSDN say this in thier code though?


fflush( stdin );   // fflush on input stream is an extension to the C standard

It says extension to C standard. I don't see that extension in my ANSI doc. Do you have that section and could post it?




Report
Re: _cgets() Problem Posted by stober on 14 Jul 2005 at 2:24 PM
: It says extension to C standard. I don't see that extension in my ANSI doc. Do you have that section and could post it?
:


that's why its called an extension -- because it is not in the ANSI doc.
Report
Re: _cgets() Problem Posted by shaolin007 on 14 Jul 2005 at 7:08 PM

that's why its called an extension -- because it is not in the ANSI doc.



Confusing language in my opinion. When I think of extension, I think of a part of something that has expanded, but in this case I guess I am wrong. Either way I believe, we have both strayed from the original topic which is not beneficial to the original poster.


Report
Re: _cgets() Problem Posted by BitByBit_Thor on 16 Jul 2005 at 9:36 AM
:
: Confusing language in my opinion. When I think of extension, I think of a part of something that has expanded, but in this case I guess I am wrong. Either way I believe, we have both strayed from the original topic which is not beneficial to the original poster.
:

:
:

I find the conversation interesting, but you are right: it doesn't really help me solve my problem:
Why does _cgets() fail? Or, if that cannot be answered:
What function do I use for user input?

Thanks for all the replies anyway... Guess Microsoft decided to 'expand' the C standard...

Greets...
Richard

Report
Reached max limit replying here Posted by shaolin007 on 18 Jul 2005 at 10:03 AM

I find the conversation interesting, but you are right: it doesn't really help me solve my problem:
Why does _cgets() fail? Or, if that cannot be answered:
What function do I use for user input?

Thanks for all the replies anyway... Guess Microsoft decided to 'expand' the C standard...

Greets...
Richard



Hey Richard, try this and see if it works. I read it somewhere on the net. By the way, fflush() wouldn't of worked anyways since _gets() is console input not stream so thats what lead me to this solution. I would still like to recommend using fgets(). In my opinion, it a far better choice to get input.

_cgets(buffer);
_getch();      /* gets rid of the '\n' according to what I have read */




Report
Re: Reached max limit replying here Posted by BitByBit_Thor on 19 Jul 2005 at 8:40 AM
:
: I find the conversation interesting, but you are right: it doesn't really help me solve my problem:
: Why does _cgets() fail? Or, if that cannot be answered:
: What function do I use for user input?
:
: Thanks for all the replies anyway... Guess Microsoft decided to 'expand' the C standard...
:
: Greets...
: Richard
:

:
:
: Hey Richard, try this and see if it works. I read it somewhere on the net. By the way, fflush() wouldn't of worked anyways since _gets() is console input not stream so thats what lead me to this solution. I would still like to recommend using fgets(). In my opinion, it a far better choice to get input.
:
:
: _cgets(buffer);
: _getch();      /* gets rid of the '\n' according to what I have read */
: 

:
:

:
:

...Actually, that doesn't work :-S
I tried this code:
_getch();
_getch();
_cgets(buffer);
_getch();
_getch();

Just to be sure, and it still seems to fail. This is the weirdest bug I have ever seen!
I'll try something again later, because currently I am working on a different computer... Thanks for your help!

Greets...
Richard




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.