newbie problem with scanf 
Author Message
 newbie problem with scanf

Hello:

There's probably a really simple reason why this program does't work the way
I want it to.  Maybe you could tell me why my last call to printf, the one
which is supposed to print the largest integer entered, is always skipped?

Thank You,

mystified

--------------------------------------------

#include <stdio.h>

int main() {

int next = 0;
int latest = 0;
int largest = 0;

printf("Enter a stream of integers: \n");
printf("Press CTRL-Z to quit.\n");

while (scanf("%d", &next)!= EOF) {
  if (next > latest) {
   largest = next;
   latest = next;
   next = 0; }
   }

printf("The largest integer is: %d\n", largest); ----->  this line is always
skipped
return 0;

Quote:
}



Wed, 20 Sep 2000 03:00:00 GMT  
 newbie problem with scanf


: Hello:

: There's probably a really simple reason why this program does't work the way
: I want it to.  Maybe you could tell me why my last call to printf, the one
: which is supposed to print the largest integer entered, is always skipped?

: Thank You,

: mystified

: --------------------------------------------

: #include <stdio.h>

: int main() {

: int next = 0;
: int latest = 0;
: int largest = 0;

: printf("Enter a stream of integers: \n");
: printf("Press CTRL-Z to quit.\n");

: while (scanf("%d", &next)!= EOF) {
:   if (next > latest) {
:    largest = next;
:    latest = next;
:    next = 0; }
:    }

: printf("The largest integer is: %d\n", largest); ----->  this line is always
: skipped
: return 0;
: }

This seems to work; the only thing I can think of is that your
compiler is trapping on the ^Z, so that the final lines aren't
executed.

Will



Wed, 20 Sep 2000 03:00:00 GMT  
 newbie problem with scanf


Quote:
> while (scanf("%d", &next)!= EOF) {

You're testing the result of scanf() incorrectly.  scanf() returns the
number of things it managed to match in the format string, so in this
case you want to say:

   while (scanf("%d", &next) == 1) {

which will run while scanf() manages to read a value in.  The program
will stop if a letter is typed, or it hits EOF.

Quote:
>   if (next > latest) {
>    largest = next;
>    latest = next;

largest and latest will pretty much always have the same value during
your program.  This doesn't stop it working, but it does suggest
something you might want to do.

Quote:
>    next = 0; }

You don't need to reset the value of next - scanf() doesn't care what
it is, it will just overwrite it.

Quote:
>    }

--

            http://www.tardis.ed.ac.uk/~broonie/
EUFS        http://www.ed.ac.uk/~filmsoc/


Sat, 23 Sep 2000 03:00:00 GMT  
 newbie problem with scanf


:
:> while (scanf("%d", &next)!= EOF) {
:
:You're testing the result of scanf() incorrectly.  scanf() returns the
:number of things it managed to match in the format string, so in this
:case you want to say:
:
:   while (scanf("%d", &next) == 1) {

Mark's advice is sound, although somewhat misleading, since scanf
does "return the value of the macro EOF if an input error occurs
before any conversion."  Since it almost always more useful to test
for conversion of the right number of input items, it is rare that a
test against EOF is seen.



Sun, 24 Sep 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie problem w/ scanf

2. scanf problem (DATA/MEMORY CONCEPT) (newbie)

3. Newbie Array/Scanf Problem

4. Newbie - while loop & scanf problems

5. SCANF() NEWBIE

6. Newbie: Trouble with scanf - HELP!!!

7. Newbie with scanf question

8. Trouble with scanf() (newbie here)

9. Newbie Scanf Puzzles

10. Newbie Array/Scanf question

11. Newbie - scanf and while loop - correction

12. scanf or n/scanf

 

 
Powered by phpBB® Forum Software