trouble with scanf? 
Author Message
 trouble with scanf?

Hi,
  i've recently been having trouble with my program which prompts for a
floating point number with scanf, and calculates some values from those
numbers. my problem is the fact that i want to be able to prompt the
input again if the user inputs something invalid (like a character or
something), or quit if the user types 'q'. the problem is, if i type a
'q' or anything besides a number when i prompts me, it skips the rest of
the scanf's! does anyone know why this could be happening? if so, please
help! thanks in advance.

victor dods



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

Quote:

>Hi,
>  i've recently been having trouble with my program which prompts for a
>floating point number with scanf, and calculates some values from those
>numbers. my problem is the fact that i want to be able to prompt the
>input again if the user inputs something invalid (like a character or
>something), or quit if the user types 'q'. the problem is, if i type a
>'q' or anything besides a number when i prompts me, it skips the rest of
>the scanf's! does anyone know why this could be happening? if so, please
>help! thanks in advance.

As you have found, scanf() is unpleasant for direct user input (although it is
usually the first (or only) input function a beginner is introduced to.)

If it is expecting a number but receives a letter, it puts that letter back in
the "input queue", where it will find it next time you call scanf().

A better input method is to use fgets() to get a whole line (including the
'\n'), then you can check the input at your leisure, and convert it to a number
with sscanf() or other functions.

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
or: http://vancouver-webpages.com/peter/index.html



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

Quote:

> Hi,
>   i've recently been having trouble with my program which prompts for a
> floating point number with scanf, and calculates some values from those
> numbers. my problem is the fact that i want to be able to prompt the
> input again if the user inputs something invalid (like a character or
> something), or quit if the user types 'q'. the problem is, if i type a
> 'q' or anything besides a number when i prompts me, it skips the rest of
> the scanf's! does anyone know why this could be happening? if so, please
> help! thanks in advance.

> victor dodsThis has to do with the way that scanf is implemeneted.  scanf assumes that the data is correct and

valid.  When the data is not valid, scanf will leave the charcters entered in the inputbuffer.  
Subsequently, the next scanf statement will read what is in the input buffer.

Try either res = scanf (......); and then query the value of res (read up on the return value of
scanf)

Another choice is to flush the buffer after the scanf . Use flushall() or fflush().

A third choice would be to read each input charcters and translate to a floalting point number.  Use  
getche() or getchar() to read each character.  If q is entered, then exit, otherwise keep reading
digits and a period until you get a whitespace charcter (tab, return, space)

Good Luck

Sa'ad Raouf



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

Quote:

> Hi,
>   i've recently been having trouble with my program which prompts for a
> floating point number with scanf, and calculates some values from those
> numbers.

  The FAQ has a good discussion of why you should avoid using scanf().  
It's very difficult to use robustly.

Quote:
> my problem is the fact that i want to be able to prompt the
> input again if the user inputs something invalid (like a character or
> something), or quit if the user types 'q'. the problem is, if i type a
> 'q' or anything besides a number when i prompts me, it skips the rest of
> the scanf's!

  Remove your scanf() and switch to fgets().  Parse the string read by
fgets() for invalid input and convert valid input to floating point
format with atof(), strtod() or sscanf().

  Hope this helps,

-- James

      A highly controversial study released today postulates a distant
      evolutionary relationship between bureaucrats and Homo Sapiens.



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

Quote:


> > Hi,
> >   i've recently been having trouble with my program which prompts for a
> > floating point number with scanf, and calculates some values from those
> > numbers.
> > victor dodsThis has to do with the way that scanf is implemeneted.  scanf assumes that the data is correct and
> valid.  When the data is not valid, scanf will leave the charcters entered in the inputbuffer.  
> Subsequently, the next scanf statement will read what is in the input buffer.

  Could you please a) hit return at least once before you start typing, and
b) either hit <CR> every 75 characters, or set your newsreader to do so
(this is sometimes called "hard wrap").  Your posts will then be _much_
easier to read.

Quote:
> Try either res = scanf (......); and then query the value of res (read
> up on the return value of scanf)

  That's possible.

Quote:
> Another choice is to flush the buffer after the scanf . Use flushall()
> or fflush().

  flushall() is not a C function (it's an extension provided by your
compiler).  The closest C equivalent is fflush(NULL).  However, since
fflush() is only defined on _output_ streams it cannot be (portably) used
to clear the input buffer.

Quote:
> A third choice would be to read each input charcters and translate to
> a floalting point number.  Use getche() or getchar() to read each
> character.  If q is entered, then exit, otherwise keep reading
> digits and a period until you get a whitespace charcter (tab, return, space)

  getche() is not a C function (it's another compiler extension), but
getchar() is.

  A fourth (and very common) choice is to use fgets() to read a whole
line of user input, which the program can dissect at leisure. :-)  This
also happens to be the approach suggested in the FAQ.


/*   Indeed, C++ is a bit of an oddball of a language ... given the way that *
 * it requires private parts to be visible.  This increases the strength of  *
 * coupling dramatically...                       -- Dr. Rich Artym          */



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

: >
: > Hi,
: >   i've recently been having trouble with my program which prompts for a
: > floating point number with scanf, and calculates some values from those
: > numbers. my problem is the fact that i want to be able to prompt the
: > input again if the user inputs something invalid (like a character or
: > something), or quit if the user types 'q'. the problem is, if i type a
: > 'q' or anything besides a number when i prompts me, it skips the rest of
: > the scanf's! does anyone know why this could be happening? if so, please
: > help! thanks in advance.
: >
: > victor dodsThis has to do with the way that scanf is implemeneted.  scanf assumes that the data is correct and
: valid.  When the data is not valid, scanf will leave the charcters entered in the inputbuffer.  
: Subsequently, the next scanf statement will read what is in the input buffer.

: Try either res = scanf (......); and then query the value of res (read up on the return value of
: scanf)

: Another choice is to flush the buffer after the scanf . Use flushall() or fflush().

Before you give advice like the above you should make sure you know what
you are talking about. Quote from K&R >
"On an output stream, fflush causes any buffered but unwritten data to be
written; on an input stream, the effect is undefined"
Undefined means undefined, anything could happen thus you should not do it.
Also flushall() is an MSDOS specific function.
: A third choice would be to read each input charcters and translate to a floalting point number.  Use  
: getche() or getchar() to read each character.  If q is entered, then exit, otherwise keep reading
: digits and a period until you get a whitespace charcter (tab, return, space)

why not just use fgets() as getche() is MSDOS specific and Victor is
looking for a function that will read a newline terminated string.
You can pick the line read by fgets() apart with sscanf() afterwards.

gabor.
--
---------------------------------------------------------------------
No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare


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



Mon, 07 Dec 1998 03:00:00 GMT  
 trouble with scanf?

Quote:

>Another choice is to flush the buffer after the scanf . Use flushall() or
> fflush().

Except of course that flushall() is not a C library function and fflush()
can't be used on input streams. Check the FAQ.

Quote:
>A third choice would be to read each input charcters and translate to a
> floalting point number.  Use  
>getche() or getchar() to read each character.  If q is entered, then exit,

getche() isn't a C library function either.

Quote:
> otherwise keep reading
>digits and a period until you get a whitespace charcter (tab, return, space)

The normal approach is to read a line with fgets and use functions such
as sscanf, atof or strtod to convert to a double. If the conversion fails
you've already removed the rest of the line from the input stream (barring
issues over very long lines) and the next read operation will read the
next line.

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


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



Mon, 07 Dec 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

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

2. Trouble with scanf() (newbie here)

3. trouble bulltproofing scanf

4. Trouble with scanf( "...%[^...]", ...)

5. scanf troubles

6. printf/scanf trouble on SPARCstation

7. Troubles using C's scanf Function

8. Scanf troubles

9. scanf or n/scanf

10. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

11. (novice) I'm Having trouble scanf'ing a double ?

12. scanf("%s", string) or scanf("%s", &string)? Both work, yet...

 

 
Powered by phpBB® Forum Software