Thread: Error Checking for scanf

  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
    39,659
    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.

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, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM