linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jesse Ruffin <jesse@ajp-services.net>
To: linux-c-programming@vger.kernel.org
Subject: Re: Help: need to prevent infinite loop
Date: Tue, 17 Jan 2006 02:14:53 -0500	[thread overview]
Message-ID: <43CC996D.4050100@ajp-services.net> (raw)
In-Reply-To: <200601171006.36978.samjnaa@gmail.com>

The problem is a general one that will affect many non digit characters.

scanf() on my computer returns 0, an early match error. But what you
probably don't expect, and I can't find in the info or man pages, is
that glibc scanf seems to unget() the input that it has read.

This means that that arrow gets put back in the stream, your program
loops, reads the same character again, and has the same matching errors
etc. etc. on into infinity.

What is you need to do is clear the stream between iterations.
This inelegant, but satisfactory, method fixes the problem:

#include "stdio.h"
int repeatinput;
int jday, jmon, jyear;
enum boolean { FALSE, TRUE };

/* Just large enough for YYYY-MM-DD + \n + \0 */
char buf[12];

void main(void)
{
	do
	{
		repeatinput = FALSE;
		printf("Date as YYYY-MM-DD     : ");

		/* Unequivocally get a whole line */
		fgets(buf,12,stdin);

		/* Scan the buffer instead */
		if (sscanf(buf, "%d-%d-%d", &jyear, &jmon, &jday) < 1)
			repeatinput = TRUE;
		if (jyear < 1800 || jyear > 2399)
			repeatinput = TRUE;
		if (repeatinput)
			printf ("Please enter a valid date.\n");
	}
	while (repeatinput);
	printf ("Thanks for entering a valid date.\n");
}

  reply	other threads:[~2006-01-17  7:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-17  4:36 Help: need to prevent infinite loop Shriramana Sharma
2006-01-17  7:14 ` Jesse Ruffin [this message]
2006-01-18 16:24   ` Shriramana Sharma
2006-01-18 16:33     ` Steve Graegert
2006-01-22 10:20   ` Glynn Clements
2006-01-22 11:28     ` Jesse Ruffin
2006-01-23  5:57       ` Jesse Ruffin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43CC996D.4050100@ajp-services.net \
    --to=jesse@ajp-services.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).