[Wylug-help] C++ code
Roger Greenwood
rg at nthong.freeserve.co.uk
Tue, 29 Oct 2002 07:02:02 +0000
Roger Leigh wrote:
>
> Roger Greenwood <rg@nthong.freeserve.co.uk> writes:
>
> > The problem is buffers (i think). The code snippet following gets a
> > number from stdin, by way of any string entry. The commented out bits
> > have been left in so you can see where I am.
> >
> > What happens is that the number entered seems to leave an entry in the
> > buffer so that the next question is already answered (incorrectly).
>
> The buffer isn't cleared when you have read from it, which shouldn't
> be a problem. However, I've seen this in the past, but can't remember
> the reason (I think it's the line feed at the end of the string).
> I've attached the code I use in gimp-print (for the escputil tool) at
> the end of the message. This code has fully tested and working
> fgets() and readline() modes, which are fully commented (it's fairly
> obvious which parts use fgets()). You'll also need to remove the _()
> i18n bits. In case you wonder why input is static, that's so the
> caller doesn't need to care about memory allocation, but they can
> duplicate it if they need.
>
> I would suggest using libreadline instead of gets/fgets, but you
> *must* have GPL licensed your code to link with it.
I'm having more success with :-
cin.getline(entry,5);
>
> >From an aesthetic point-of-view, I don't like the TRUE and FALSE
> macros; just use if (valid) or if (!valid) which are much safer and
> easier to read (if TRUE is 0, FALSE is !0, not 1 and vice-versa). The
> definition of true and false can also change:
Yes they are defined :-
//******************************************************
// # DEFINES and global variables
//******************************************************
#define TRUE 1
#define FALSE 0
This code was written some time ago so I'm sorry I got it wrong - I
think I defined it like that because there was no defined standard that
I knew about.
>
> if (exit)
> exit(EXIT_SUCCESS);
> Here, exit is 1, which is FALSE!! From the code above, I guess you
> used the opposite values, which is /not/ obvious!
>
> I would also check the return type of /every/ libc function (except
> perhaps printf) and print a diagnostic error message if it returns a
> failure:
>
> fprintf(stderr, "%s: Foo happened: %s\n", argv[0], strerror(errno));
> (If you only use GNU ld/GNU libc, you can use program_invocation_name
> and program_invocation_short_name instead of argv[0].)
>
> HTH,
> Roger
Thanks for your comments.
I have never heard of libreadline.
RG