Need assistance with detecting improper input

Discussion in 'C Programming' started by david.w.anderson1, Nov 27, 2005.

  1. This is a homework assignment.
    The code works but does not "catch" all the improper input.
    The requirement is to be able to accept one input currency, which is
    error checked as a valid entry, and then display its equivalency in US

    The program does detect when a letter is entered as the 1st digit.
    I need the program to detect when a number-letter combination is
    entered (example 1a) and show the error.

    I need the program to detect when a comma is entered (example: 12,34
    instead of 12.34) and show the error.

    Thanks in advance.

    Here is the code:

    #include <stdio.h>
    #include <system.h>
    #include <stdlib.h>
    #include <ctype.h>

    #define PHP 54.36000
    #define AFA 45.54600
    #define KRW 1051.50
    #define JPY 118.740
    #define GBP 0.58011
    #define TRUE 1
    #define FALSE !TRUE
    int main(void)

    int intNum;
    char userInput[20];
    float USD;

    printf("CURRENCY CONVERSION\n");
    printf("This program will show currency conversions to US
    printf("1 US Dollar = %4.2f \t Phillipine Pesos\n", PHP);
    printf("1 US Dollar = %4.2f \t Afghanistan Afghani\n", AFA);
    printf("1 US Dollar = %4.2f \t Korean Won\n", KRW);
    printf("1 US Dollar = %4.2f \t Japanese Yen\n", JPY);
    printf("1 US Dollar = %4.2f \t British Pounds\n\n\n", GBP);
    printf("Press any key to continue...\n\n");
    getch (); // Waits for keystroke before proceeding

    printf("To show an example of this program, we will convert:\n");
    printf("British Pounds to U.S. Dollars.\n\n");
    printf("Enter the amount of British Pounds to convert then select
    the ENTER key:\n\n");
    getch ();

    USD = atof(userInput);

    if( toupper(userInput[0])=='Q')
    printf("\n\n\nThank you for using this program.\n");
    printf("Please press any key to close this window\n");

    else if( USD <= 0.0)
    printf("\nYou have entered an invalid amount. Your
    entry was: %s\n",userInput);
    printf("Please enter a valid amount and then select the
    ENTER key:\n");
    printf("You entered %s British Pounds:\n\n", userInput);
    printf("The conversion of %s British Pounds to US
    Dollars is:\n\n\n", userInput);
    printf("\t\t $%.2f\n\n\n\n", USD / GBP);
    printf("Enter another amount or press q to exit.\n");

    david.w.anderson1, Nov 27, 2005
    1. Advertisements

  2. A non-standard header which will prevent others from helping and from
    which you receive almost no benefit.
    Why use a non-standard function when getchar is available?
    There is no way to prevent gets from overflowing your buffer if the
    user enters too much input. Consider using fgets.
    atof provides no information about any errors it encounters. Look up
    the strtod() function in your manual. It has features which allow you
    to do what you want.
    This is a little late. You should do this before attempting to
    convert the input to a numeric value.
    This will change when you use strtod.

    <<Remove the del for email>>
    Barry Schwarz, Nov 27, 2005
    1. Advertisements

  3. david.w.anderson1

    Malcolm Guest

    Make this
    fgets(userInput, 20, stdin);
    Now if the user types more than 20 characters, the buffer will fill up and
    there will be no newline (with gets() the buffer will overflow and the
    computer will probably crash, maybe give a confusing error, depending what
    gets hits).
    So check.
    if(strchr(userInput, '\n') == 0)
    fprintf(stderr, "Line too long"):
    /* easiest thing is to quit, but you can try to read the rest of the
    line if you want */
    /* I'll leave that for you you implement */
    atof() won't catcvh errors
    declare a char *endptr.
    USD = strtod(userInput, &endptr);
    The endptr points to then end of the readable, floating point input. If is
    is whitespace or NUL, you are OK, if it contains printable characters then
    user typed a number followed by garbage. If endptr equals the start, the
    user typed garbage, maybe followed by a valid number, maybe not.
    if(endptr == userInput)
    /* cannot convert at all */
    if( endptr != 0 && isspace(*endptr) == 0)
    /* garbage after number */
    Malcolm, Nov 27, 2005
  4. On 26 Nov 2005 19:22:34 -0800, in comp.lang.c ,
    Don't use gets(), and avoid the ato... family.

    Read the FAQ for the right way to do this. By the way it will also
    help your problem of dealing with invalid input.
    Mark McIntyre, Nov 27, 2005
  5. Thank you all for the responses to my problem.
    You have given me some great advice and guidance.
    Much appreciated.
    david.w.anderson1, Nov 27, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.