Error Checking for scanf

This is a discussion on Error Checking for scanf within the C Programming forums, part of the General Programming Boards category; Code: do { printf("\n\nPlease Enter Current US Dollar Exchange Rate: \t"); error = scanf("%f",&USD); fflush(stdin); } while ( error == ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Error Checking for scanf

    Code:
    do
                {
                	printf("\n\nPlease Enter Current US Dollar Exchange Rate: \t");
                	error = scanf("%f",&USD);
                    fflush(stdin);
                } 	while ( error == 0 || USD <= 0);

    What Else can I add here to eliminate letters and combinations of letters and numbers? For example if i enter a number followed by a string of letters: how can i return that case as an error?

    Thanks in advance,

    Dave!

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    people suggest that you use fgets and the sscanf combination. You might want to look into that.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    See previous thread
    Error Checking
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 11:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21