vippstar
8/9/2008 12:46:00 PM
On Aug 9, 3:28 pm, "raseelbha...@gmail.com" <raseelbha...@gmail.com>
wrote:
> Hi,
> I am writing a simple program in which I am using scnaf() twice. The
> pseudo code of the program is as follows :
>
> ...
> printf("Enter lesson no.:");
add fflush(stdout); after printf here.
> scanf("%d",lesson);
change this to %4d and check the return value of scanf
if((rc = scanf("%4d", lesson)) != 1)
by the way, is 'lesson' really a pointer to int, or plain int? Latter
case, change it to &lesson.
> ..
> fp = fopen("questions.txt","r");
if(fp == NULL) /* handle error */
> while(fgets(buf, sizeof(buf), fp) {
> fputs(buf, stdout);
> }
>
> fclose(fp);
> printf("Choose answer\n");
> scanf("%c", &c);
Most probably what you have entered before in the first scanf is this:
42<RET>
The <RET> leaves a newline in the stream. the first scanf sees this
and stops reading the integer, leaving the newline in the stream.
Then the second scanf reads the newline instead of waiting for input.
To fix this, before the second scanf, add these two lines:
scanf("%*[^\n]");
getchar();
<snip rest code and questions>