Need assistance with detecting improper input

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

  1. Guest

    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
    dollars.

    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
    Dollars.\n\n");
    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 ();

    while(TRUE)
    {
    gets(userInput);
    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");
    getch();
    break;
    }

    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");
    }
    else
    {
    printf("\n\n\n\n\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");
    }

    }
    }
    , Nov 27, 2005
    #1
    1. Advertising

  2. On 26 Nov 2005 19:22:34 -0800, wrote:

    >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
    >dollars.
    >
    >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>


    A non-standard header which will prevent others from helping and from
    which you receive almost no benefit.

    >#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
    >Dollars.\n\n");
    > 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


    Why use a non-standard function when getchar is available?

    >
    > 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 ();
    >
    > while(TRUE)
    > {
    > gets(userInput);


    There is no way to prevent gets from overflowing your buffer if the
    user enters too much input. Consider using fgets.

    > USD = atof(userInput);


    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.

    >
    > if( toupper(userInput[0])=='Q')


    This is a little late. You should do this before attempting to
    convert the input to a numeric value.

    > {
    > printf("\n\n\nThank you for using this program.\n");
    > printf("Please press any key to close this window\n");
    > getch();
    > break;
    > }
    >
    > else if( USD <= 0.0)


    This will change when you use strtod.

    > {
    > 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");
    > }
    > else
    > {
    > printf("\n\n\n\n\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");
    > }
    >
    > }
    >}



    <<Remove the del for email>>
    Barry Schwarz, Nov 27, 2005
    #2
    1. Advertising

  3. Malcolm Guest

    <> wrote

    > gets(userInput);
    >

    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 */
    exit(EXIT_FAILURE);
    }
    >
    > USD = atof(userInput);
    >

    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 */
    > 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");
    > getch();
    > break;
    > }
    >
    > 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");
    > }
    > else
    > {
    > printf("\n\n\n\n\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");
    > }
    >
    > }
    > }
    >
    Malcolm, Nov 27, 2005
    #3
  4. On 26 Nov 2005 19:22:34 -0800, in comp.lang.c ,
    wrote:

    > gets(userInput);
    > USD = atof(userInput);


    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
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Nov 27, 2005
    #4
  5. Guest

    Thank you all for the responses to my problem.
    You have given me some great advice and guidance.
    Much appreciated.
    , Nov 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmVzc3lFenp5?=

    Handling improper logout

    =?Utf-8?B?SmVzc3lFenp5?=, May 21, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    579
    =?Utf-8?B?SmVzc3lFenp5?=
    May 22, 2005
  2. sp0
    Replies:
    10
    Views:
    2,419
    Buster
    Mar 4, 2004
  3. The Antarctican

    Need assistance pertaining to <form> & <input>

    The Antarctican, Jun 25, 2012, in forum: HTML and CSS
    Replies:
    0
    Views:
    662
    The Antarctican
    Jun 25, 2012
  4. The Antarctican

    Need assistance with SUBMIT & INPUT

    The Antarctican, Jun 25, 2012, in forum: Java
    Replies:
    0
    Views:
    340
    The Antarctican
    Jun 25, 2012
  5. The Antarctican

    Need assistance with <form> & <input>

    The Antarctican, Jun 25, 2012, in forum: Javascript
    Replies:
    0
    Views:
    1,042
    The Antarctican
    Jun 25, 2012
Loading...

Share This Page