View Full Version : use of scanf in a while loop
Hai,
i am having a problem while using scanf in a while loop.I tried in many ways but couldnt get solution.Please see the coding which i am giving below
and hope i will get some solution for that.
:-?
while(1)
{
printf("Enter any number \n");
scanf("%d",&i);
printf("do u want to continue,press (y/n)\n");
scanf("%c",&ch);
if(ch=='n')
break;
}
I want the program so that if 'y' is pressed loop should continue,else should break.But scanf is not taking any character and loop is breaking.
waiting for reply,
usha.
RobSeace
11-30-2005, 02:11 PM
scanf() is a very poor choice for reading user input, in general... I'd recommend
using something else... Even if you just switch to fgets() + sscanf() instead, it'd
still be a vast improvement over plain old scanf(), because at least then you'd be
guaranteed to be working off discrete whole lines of entered data...
But, if you're expecting that just hitting Y or N is going to work, it won't; you'll need
to hit ENTER before your scanf() will ever see what you entered... Also, since you
probably hit ENTER after entering the numbers for the first scanf(), then there will
be a queued up newline waiting to be read, so that scanf() will probably succeed
right away and "ch" will be set to '\n'... Like I say, scanf() is NOT a good choice for
reading user input...
Thank u
but i want to u to write the code also for me so that i will get a clear idea of writing the program.Bye
RobSeace
12-01-2005, 01:35 PM
but i want to u to write the code also for me
Yes, and there are quite a few things which *I* want that I could name for you as
well, but I'd be highly unlikely to receive them anytime soon... ;-)
Plus, I don't even know exactly what kind of behavior you're looking for in your
interface... Do you want the Y/N to be single key-press only, or do you want them
to have to hit ENTER? Just to do things the absolutely simplest way possible, and
requiring the ENTER, then like I said, go with fgets()... *sigh* Ok, fine, here's some
code... I'm just too damned nice for my own good, sometimes... ;-)
int num;
char buf[128];
for (;;) {
fputs ("Enter a number: ", stderr);
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
num = atoi (buf);
/* ... presumably something useful is done with num here?? ... */
fputs ("Continue? (Y/n) ", stderr);
if ((fgets (buf, sizeof (buf), stdin) == NULL) || (toupper (buf[0]) == 'N'))
break;
}
Yes, and there are quite a few things which *I* want that I could name for you as
well, but I'd be highly unlikely to receive them anytime soon... ;-)
Plus, I don't even know exactly what kind of behavior you're looking for in your
interface... Do you want the Y/N to be single key-press only, or do you want them
to have to hit ENTER? Just to do things the absolutely simplest way possible, and
requiring the ENTER, then like I said, go with fgets()... *sigh* Ok, fine, here's some
code... I'm just too damned nice for my own good, sometimes... ;-)
int num;
char buf[128];
for (;;) {
fputs ("Enter a number: ", stderr);
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
num = atoi (buf);
/* ... presumably something useful is done with num here?? ... */
fputs ("Continue? (Y/n) ", stderr);
if ((fgets (buf, sizeof (buf), stdin) == NULL) || (toupper (buf[0]) == 'N'))
break;
}
Hai,
Thank u for the code which u have given me.Now i am working with my program in a very good way .But still i cant understand why i couldnt get through scanf.Can u please tell me that.
RobSeace
12-02-2005, 12:43 PM
Um, I thought I already DID tell you:
Also, since you
probably hit ENTER after entering the numbers for the first scanf(), then there will
be a queued up newline waiting to be read, so that scanf() will probably succeed
right away and "ch" will be set to '\n'...
In other words, when you do scanf("%c"), it doesn't skip leading whitespace, as
most *scanf() formats do, but will instead just give you whatever character is next
to be read, even if it's a whitespace char... So, the newline char that followed your
number entry would be what you were reading...
Its simple, use flush(stdin) after the scanf(), things should work. Because after scanf() '\n' remains in the buffer and getchar() gets it. Enjoy!!
RobSeace
02-28-2008, 12:28 PM
Its simple, use flush(stdin) after the scanf(), things should work. Because after scanf() '\n' remains in the buffer and getchar() gets it. Enjoy!!
Um, no, that's completely and totally wrong... fflush(stdin) accomplishes absolutely
nothing... Zero, nada, zilch... fflush() only does anything on file pointers open for
WRITING, and simply flushes any buffered unwritten data out... It does NOTHING
related to input at all... (Well, technically, it might reposition the true file offset to
where you're currently at in the read, if working with a real file... But, it's sure not
going to just throw away unread input data sitting in the buffer!)
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.