Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C

Second instance of scnaf not working

 
Jump to:  
 
raseelbhagat@gmail.com
PostPosted: Sat Aug 09, 2008 12:28 pm    Post subject: Second instance of scnaf not working
       
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.:");
scanf("%d",lesson);
...
fp = fopen("questions.txt","r");
while(fgets(buf, sizeof(buf), fp) {
fputs(buf, stdout);
}
fclose(fp);
printf("Choose answer\n");
scanf("%c", &c);
if (c == 'A')
printf("Right Answer\n");
else
pritnf("Wrong answer\n");
.....

In the above code, if I don't use the first scanf(), everything works
properly.
Otherwise, after entering an int for the first scanf, the program just
"falls through" without waiting for user input for the second scanf()
and printing "Wrong Answer".

Am I missing out on something here ?
 

 
pete
PostPosted: Sat Aug 09, 2008 12:28 pm    Post subject: Re: Second instance of scnaf not working
       
raseelbhagat@gmail.com wrote:
Quote:
Hi,

pseudo code

That's a poor choice to post to the C language newsgroup.

Quote:
...
printf("Enter lesson no.:");
scanf("%d",lesson);
..
fp = fopen("questions.txt","r");
while(fgets(buf, sizeof(buf), fp) {
fputs(buf, stdout);
}
fclose(fp);
printf("Choose answer\n");
scanf("%c", &c);
if (c == 'A')
printf("Right Answer\n");
else
pritnf("Wrong answer\n");
....

In the above code, if I don't use the first scanf(), everything works
properly.
Otherwise, after entering an int for the first scanf, the program just
"falls through" without waiting for user input for the second scanf()
and printing "Wrong Answer".

Am I missing out on something here ?

/* BEGIN new.c output */

Enter lesson no.:7
Choose answer
A
Right Answer

/* END new.c output */



/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int lesson[1];
char c;

puts("/* BEGIN new.c output */\n");
printf("Enter lesson no.:");
fflush(stdout);
if (scanf("%d", lesson) != 1) {
puts("Adios Amiga!");
exit(EXIT_FAILURE);
}
getchar();
puts("Choose answer");
if (scanf("%c", &c) != 1) {
puts("Adios Amoeba!");
exit(EXIT_FAILURE);
}
getchar();
if (c == 'A') {
puts("Right Answer");
} else {
puts("Wrong answer");
}
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */


--
pete
 

 
raseelbhagat@gmail.com
PostPosted: Sat Aug 09, 2008 12:44 pm    Post subject: Re: Second instance of scnaf not working
       
On Aug 9, 5:28 pm, "raseelbha...@gmail.com" <raseelbha...@gmail.com>
wrote:
Quote:
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.:");
scanf("%d",lesson);
..
fp = fopen("questions.txt","r");
while(fgets(buf, sizeof(buf), fp) {
   fputs(buf, stdout);}

fclose(fp);
printf("Choose answer\n");
scanf("%c", &c);
if (c == 'A')
    printf("Right Answer\n");
else
    pritnf("Wrong answer\n");
....

In the above code, if I don't use the first scanf(), everything works
properly.
Otherwise, after entering an int for the first scanf, the program just
"falls through" without waiting for user input for the second scanf()
and printing "Wrong Answer".

Am I missing out on something here ?


An additional progress to my above conundrum is : Substituting "%s"
with "%c" in the second scanf() solves the problem.
But I don't understand, shouldn't "%c" have worked too ?
 

 
Guest
PostPosted: Sat Aug 09, 2008 12:45 pm    Post subject: Re: Second instance of scnaf not working
       
On Aug 9, 3:28 pm, "raseelbha...@gmail.com" <raseelbha...@gmail.com>
wrote:
Quote:
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.

Quote:
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.

Quote:
..
fp = fopen("questions.txt","r");

if(fp == NULL) /* handle error */

Quote:
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>
 

 
Guest
PostPosted: Sat Aug 09, 2008 12:51 pm    Post subject: Re: Second instance of scnaf not working
       
On Aug 9, 3:44 pm, "raseelbha...@gmail.com" <raseelbha...@gmail.com>
wrote:
<snip>

Quote:
An additional progress to my above conundrum is : Substituting "%s"
with "%c" in the second scanf() solves the problem.
But I don't understand, shouldn't "%c" have worked too ?

See my other reply. Before the second scanf, the stream contains a
newline.
's' ignores all isspace bytes. (\n, ' ', \t ...)
 

 
CBFalconer
PostPosted: Sat Aug 09, 2008 1:56 pm    Post subject: Re: Second instance of scnaf not working
       
"raseelbhagat@gmail.com" wrote:
Quote:

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.:");
scanf("%d",lesson);
..
fp = fopen("questions.txt","r");
while(fgets(buf, sizeof(buf), fp) {
fputs(buf, stdout);
}
.... snip ...

In the above code, if I don't use the first scanf(), everything
works properly. Otherwise, after entering an int for the first
scanf, the program just "falls through" without waiting for user
input for the second scanf() and printing "Wrong Answer".

Am I missing out on something here ?

Yes. First, each scanf should have the result checked. Also, each
should be followed by absorbing the remainder of the input line.
Change the first section to:

printf("Enter lesson no.:"); fflush(stdout);
if (1 != scanf("%d",lesson))
badinputerrorhandle();
else {
flushln(stdin);
(* success, continue *)

and the very useful flushln function is:

/* flush input through the next following '\n' inclusive */
int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f)) && (EOF != ch)) continue;
return ch;
}

This treatment should be used on any interactive use of scanf.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Szafy metalowe okna słupsk preflop tanie pozycjonowanie upominki