scanf in a for loop

G

Guest

In Stephen G. Kochan's book "Programming in C" there is a program with the
following:

for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
scanf("i%", &grade);
...
}

Does the scanf command pause the loop until the user input is received?
When I run this program the loop keeps on going without pausing for the
user input.

What is going on here? Can anyone please help me?

JoshO.
 
S

skaoth

Yes, the scanf should cause the for loop to pause and allow you to
enter your integer.
The syntax error that I see is that
scanf("i%", &grade);
should be
scanf("%i", &grade);
 
M

Malcolm

wrote
for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
scanf("i%", &grade);
...
}

Does the scanf command pause the loop until the user input is received?
When I run this program the loop keeps on going without pausing for the
user input.

What is going on here? Can anyone please help me?
Yoy've mistyped the "%i" as "i%". This means that scanf() is looking for the
letter I followed by the field specifier, followed by a NUL, which is
illegal. On your system it is simply ignoring it and returning.

When calling scanf() you should check the return value, which gives the
number of fields successfully converted, mainly to guard against bad user
input but also to catch this sort of error.
 
R

reachsachin

In Stephen G. Kochan's book "Programming in C" there is a program with the
following:

for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
scanf("i%", &grade);
...
}

Does the scanf command pause the loop until the user input is received?
When I run this program the loop keeps on going without pausing for the
user input.

What is going on here? Can anyone please help me?

JoshO.
 
S

sachin

hi josho
first of all, the format specifier in the scanf statement is incorrect,
as mentioned by others.

Secondly, the scanf statement is not waiting for further values of
grade as expected just because it always find a character in its input
buffer.
So one probable solution is to clear that character, which you enter
for one scanf statement from the input buffer, so that next scanf
statement DOES NOT find the previously keyed in character, and waits
for the new fresh value of 'grade'.

for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
fflush(stdin);
scanf("i%", &grade);
....
}
I tested the code, its running on solaris platform. Moreover, I believe
fflush is a generic syscall independent of the OS.

Regards
Sachin
 
K

Keith Thompson

sachin said:
hi josho
first of all, the format specifier in the scanf statement is incorrect,
as mentioned by others.

Secondly, the scanf statement is not waiting for further values of
grade as expected just because it always find a character in its input
buffer.
So one probable solution is to clear that character, which you enter
for one scanf statement from the input buffer, so that next scanf
statement DOES NOT find the previously keyed in character, and waits
for the new fresh value of 'grade'.

for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
fflush(stdin);
scanf("i%", &grade);
...
}
I tested the code, its running on solaris platform. Moreover, I believe
fflush is a generic syscall independent of the OS.

fflush(stdin) invokes undefined behavior.

<http://www.eskimo.com/~scs/C-faq/q12.26.html>
 
K

Keith Thompson

Mac said:
printf("Enter grade #%i: ", i);
fflush(stdin);
[snip]

fflush(stdin) invokes undefined behavior.

Yes, but sachin should have written "fflush(stdout)", obviously.

Actually, I think sachin meant to clear the input characters. The way
to do that is to read and discard them. (I'm too lazy to work out the
details.)

Merry Christmas!
 
I

infobahn

Mac said:
[snip]
printf("Enter grade #%i: ", i);
fflush(stdin);
[snip]

fflush(stdin) invokes undefined behavior.


Yes, but sachin should have written "fflush(stdout)", obviously.

And he should have written corrected the OP's i% to %i in the
scanf call. And he should have demonstrated how to test the
return value from scanf properly...
 
I

infobahn

Mac said:
Mac said:
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:




[snip]


printf("Enter grade #%i: ", i);
fflush(stdin);

[snip]


fflush(stdin) invokes undefined behavior.


Yes, but sachin should have written "fflush(stdout)", obviously.

And he should have written corrected the OP's i% to %i in the
scanf call.
^^^^^
Actually, he did. It's still visible above...

No, he didn't, and no, it isn't.
 
M

Mac

Mac said:
Mac wrote:

On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:




[snip]


printf("Enter grade #%i: ", i);
fflush(stdin);

[snip]


fflush(stdin) invokes undefined behavior.


Yes, but sachin should have written "fflush(stdout)", obviously.

And he should have written corrected the OP's i% to %i in the
scanf call.
^^^^^

Actually, he did. It's still visible above...

No, he didn't, and no, it isn't.

My apologies. You are correct on both counts. I was looking at the
printf() call, as you seem to have surmised.

--Mac
 
K

king arthur

In Stephen G. Kochan's book "Programming in C" there is a program with the
following:

for(i = 1; i <= numOfGrades; ++i) {
printf("Enter grade #%i: ", i);
scanf("i%", &grade);
...
}

Does the scanf command pause the loop until the user input is received?
When I run this program the loop keeps on going without pausing for the
user input.

What is going on here? Can anyone please help me?

JoshO.

First of all I believe the scanf statement as listed above has a
slight error. The "i%" should be "%i".

Retry the program with this correction and see what happens.
 
L

Lawrence Kirby

Mac said:
printf("Enter grade #%i: ", i);
fflush(stdin); [snip]

fflush(stdin) invokes undefined behavior.

Yes, but sachin should have written "fflush(stdout)", obviously.


That is a good idea here, but isn't what he meant.
Actually, I think sachin meant to clear the input characters. The way
to do that is to read and discard them. (I'm too lazy to work out the
details.)

The problem with that is knowing when to stop. C has no way to query
whether the character read is the last available or whether there are any
more. Anyway why should a program assume that characters already there
aren't valid? That's not the cause of the problem in this case.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top