473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

scanf in a for loop

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.

Nov 14 '05 #1
15 11757
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);

Nov 14 '05 #2

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.
Nov 14 '05 #3

"" <> wrote:
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.


Nov 14 '05 #4
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

Nov 14 '05 #5
"sachin" <re*********@gmail.com> writes:
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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Mac
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
"sachin" <re*********@gmail.com> writes: [snip]
printf("Enter grade #%i: ", i);
fflush(stdin);

[snip]
fflush(stdin) invokes undefined behavior.


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

--Mac

Nov 14 '05 #7
Mac <fo*@bar.net> writes:
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
"sachin" <re*********@gmail.com> writes:

[snip]
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!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Mac wrote:
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:

"sachin" <re*********@gmail.com> writes:


[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...
Nov 14 '05 #9
Mac
On Sat, 25 Dec 2004 23:06:42 +0000, Keith Thompson wrote:
Merry Christmas!


Merry Christmas to you, too!

;-)

--Mac

Nov 14 '05 #10
Mac
On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:
Mac wrote:
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:

"sachin" <re*********@gmail.com> writes:


[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...
--Mac

Nov 14 '05 #11
Mac wrote:
On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:

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

"sachin" <re*********@gmail.com> writes:

[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.
Nov 14 '05 #12
sachin wrote on 25/12/04 :
fflush(stdin);


No. Please, read the FAQ.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #13
Mac
On Sun, 26 Dec 2004 07:25:14 +0000, infobahn wrote:
Mac wrote:
On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:

Mac wrote:

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

>"sachin" <re*********@gmail.com> writes:

[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

Nov 14 '05 #14
On Sat, 25 Dec 2004 02:48:27 -0500, "" <> wrote:
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.

Nov 14 '05 #15
On Sat, 25 Dec 2004 23:06:42 +0000, Keith Thompson wrote:
Mac <fo*@bar.net> writes:
On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
"sachin" <re*********@gmail.com> writes: [snip]
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
Nov 14 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
2
by: AR | last post by:
Hi I am trying to figure out how to set up the conditions of my for loop in a nested loop program. I am missing some key element in either or both the syntax of the for loop and/or the placement ...
6
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters...
1
by: robrobrob | last post by:
I have a C consloe application in windows using Visual Studio Pro 6.0 that gives a runtime error: Debug Error! During the scanf line. If I put a printf loop in after the scanf loop to print the...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
8
by: Army1987 | last post by:
Is this a good way to discard unread data after scanf()? while (getchar() != '\n') ; According to the FAQ scanf always leaves the trailing newline on the input stream, so there's no risk of...
18
by: Vijaykumar Dave | last post by:
I have a program for base X power N as under. The problem is that when the range specified in loop is given it works well, but when any character is pressed, it goes to infinite loop. Second...
6
by: akk003 | last post by:
Hello I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code: int loop = 1; char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.