Anybody know how I would code an error statement when a user inputs an invalid input.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jeff regoord

    Anybody know how I would code an error statement when a user inputs an invalid input.

    A user inputs a float value. The scanf() function gets the value.
    However, I need to create an error handler with an if else statement
    saying invalid input if the input is not a number. Does anybody know
    how I could do this?
  • pete

    #2
    Re: Anybody know how I would code an error statement when a user inputs an invalid input.

    jeff regoord wrote:[color=blue]
    >
    > A user inputs a float value. The scanf() function gets the value.
    > However, I need to create an error handler with an if else statement
    > saying invalid input if the input is not a number. Does anybody know
    > how I could do this?[/color]

    /* BEGIN new.c */

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

    #define STRINGLENGTH 1000
    #define str(x) # x
    #define xstr(x) str(x)

    int main(void)
    {
    char string[STRINGLENGTH + 1] = {'\0'};
    int rc;

    fputs("Enter a floating point value: ", stdout);
    fflush(stdout);
    rc = scanf("%" xstr(STRINGLENG TH) "[^\n]%*[^\n]", string);
    getchar();
    while (rc) {
    double number;
    char *endptr;

    number = strtod(string, &endptr);
    if (errno) {
    puts("Value out of range.");
    errno = 0;
    } else if (*endptr && !isspace((int)* endptr)) {
    puts("\nI'm sorry Dave. I'm afraid I can't do that.\n");
    } else {
    printf("The number is %f\n", number);
    }
    fputs("Enter a floating point value: ", stdout);
    fflush(stdout);
    rc = scanf("%" xstr(STRINGLENG TH) "[^\n]%*[^\n]", string);
    getchar();
    }
    return 0;
    }

    /* END new.c */

    Comment

    • Arthur J. O'Dwyer

      #3
      Re: Anybody know how I would code an error statement when a user inputs an invalid input.


      On Wed, 10 Sep 2003, jeff regoord wrote:[color=blue]
      >
      > A user inputs a float value. The scanf() function gets the value.
      > However, I need to create an error handler with an if else statement
      > saying invalid input if the input is not a number. Does anybody know
      > how I could do this?[/color]

      Try to be more specific. At the most basic level, you could write

      int ret;
      ...

      input_the_numbe r:
      ret = scanf("%d", &number);

      if (ret != 1) {
      printf("That was not a number; try again!\n");
      goto input_the_numbe r;
      }
      if (number < 0) {
      printf("That was negative; try again!\n");
      goto input_the_numbe r;
      }
      ...


      (Or equivalent stuff with more advanced control-flow structures, but I'm
      too tired to work out where everything would go in a 'while' loop.)

      At a more advanced level, the concept of "error handling" is a pretty
      interesting one. I myself am a fan of the following little function:


      #include <stdio.h>
      #include <stdlib.h>
      #include <stdarg.h>

      static char *Argv0;

      void do_error(const char *fmat, ...);

      int main(int argc, char *argv[])
      {
      Argv0 = argv[0];

      if (0 == 1) {
      do_error("Oh dearie me: %d %d %d\n", 0, 1, 0==1);
      }

      return 0;
      }

      /** This is the interesting bit. */

      void do_error(const char *fmat, ...)
      {
      va_list ap;
      printf("%s: ", Argv0);
      va_start(ap, fmat);
      vprintf(fmat, ap);
      va_end(ap);
      exit(EXIT_FAILU RE);
      }


      In case you can't figure it out from the code, the do_error function
      takes a variable number of arguments, just like 'printf', but before
      printing them, it tries to print the name of the executing program,
      and then, once everything is printed, it terminates the program
      directly. This is for those "no recovery possible" sorts of situations,
      which may or may not apply to your problem.

      As you may know, and unlike C++, C does *not* support exception handling
      at the language level. (Anyone got a nice throw/catch sort of idiom for
      standard C? :)

      HTH,
      -Arthur

      Comment

      • Kevin Easton

        #4
        Re: Anybody know how I would code an error statement when a user inputs an invalid input.

        Arthur J. O'Dwyer <ajo@andrew.cmu .edu> wrote:
        [...][color=blue]
        > As you may know, and unlike C++, C does *not* support exception handling
        > at the language level. (Anyone got a nice throw/catch sort of idiom for
        > standard C? :)[/color]

        This might be a good start if you wanted to look into this area...

        #include <stdio.h>
        #include <stdlib.h>
        #include <setjmp.h>

        struct ehandler { jmp_buf env; };

        struct ehandler next_up_handler ;

        #define INIT_EXCEPTIONS \
        if (setjmp(next_up _handler.env)) { \
        fprintf(stderr, "Uncaught exception, terminating.\n" ); \
        exit(EXIT_FAILU RE); \
        }

        #define I_CATCH_EXCEPTI ONS volatile int caught;

        #define try { \
        volatile struct ehandler saved_handler = next_up_handler ; \
        if (!(caught = setjmp(next_up_ handler.env)))

        #define catch \
        next_up_handler = saved_handler; \
        } if (caught) \

        #define throw longjmp(next_up _handler.env, 1)

        /* Exception-throwing version of malloc */
        void *emalloc(size_t s)
        {
        void *r = malloc(s);
        if (!r)
        throw;
        return r;
        }

        int bar()
        {
        throw;

        return 0;
        }

        int foo()
        {
        I_CATCH_EXCEPTI ONS
        void * volatile j;

        j = emalloc(100);
        try {
        bar();
        } catch {
        printf("foo caught exception, cleaning up...\n");
        free(j);
        throw;
        }

        printf("foo Normal function execution\n");
        free(j);

        return 0;
        }

        int main()
        {
        I_CATCH_EXCEPTI ONS

        INIT_EXCEPTIONS

        try {
        foo();
        } catch {
        fprintf(stderr, "main caught an exception, exiting\n");
        return EXIT_FAILURE;
        }

        printf("program completed normally.\n");

        return 0;
        }

        Obviously if you wanted to make this sort of thing more
        production-quality you'd have to use a consistent namespace for
        identifiers and that sort of thing - perhaps you'd want to allow
        multiple kinds of exceptions too.

        - Kevin.

        Comment

        • Kevin Easton

          #5
          Re: Anybody know how I would code an error statement when a user inputs an invalid input.

          I wrote:
          [...][color=blue]
          > #define I_CATCH_EXCEPTI ONS volatile int caught;
          >
          > #define try { \
          > volatile struct ehandler saved_handler = next_up_handler ; \
          > if (!(caught = setjmp(next_up_ handler.env)))
          >
          > #define catch \[/color]

          Come to think of it, it'd be better to put an empty statement in here:

          ; \

          Just in case someone puts a catch immediately after a try without an
          intervening statement. Alternatively it could just be specified that
          there has to be a statement there - there's already other restrictions
          like no return statements inside try blocks.
          [color=blue]
          > next_up_handler = saved_handler; \
          > } if (caught) \
          >
          > #define throw longjmp(next_up _handler.env, 1)
          >
          > /* Exception-throwing version of malloc */
          > void *emalloc(size_t s)
          > {
          > void *r = malloc(s);
          > if (!r)
          > throw;
          > return r;
          > }
          >
          > int bar()
          > {
          > throw;
          >
          > return 0;
          > }
          >
          > int foo()
          > {
          > I_CATCH_EXCEPTI ONS
          > void * volatile j;
          >
          > j = emalloc(100);
          > try {
          > bar();
          > } catch {
          > printf("foo caught exception, cleaning up...\n");
          > free(j);
          > throw;
          > }
          >
          > printf("foo Normal function execution\n");
          > free(j);
          >
          > return 0;
          > }
          >
          > int main()
          > {
          > I_CATCH_EXCEPTI ONS
          >
          > INIT_EXCEPTIONS
          >
          > try {
          > foo();
          > } catch {
          > fprintf(stderr, "main caught an exception, exiting\n");
          > return EXIT_FAILURE;
          > }
          >
          > printf("program completed normally.\n");
          >
          > return 0;
          > }
          >
          > Obviously if you wanted to make this sort of thing more
          > production-quality you'd have to use a consistent namespace for
          > identifiers and that sort of thing - perhaps you'd want to allow
          > multiple kinds of exceptions too.
          >
          > - Kevin.
          >[/color]

          Comment

          • goose

            #6
            Re: Anybody know how I would code an error statement when a user inputs an invalid input.

            "Arthur J. O'Dwyer" <ajo@andrew.cmu .edu> wrote in message news:<Pine.LNX. 4.55L-032.03091022435 20.4601@unix44. andrew.cmu.edu> ...[color=blue]
            > #include <stdio.h>
            > #include <stdlib.h>
            > #include <stdarg.h>
            >
            > static char *Argv0;
            >
            > void do_error(const char *fmat, ...);
            >
            > int main(int argc, char *argv[])
            > {
            > Argv0 = argv[0];
            >
            > if (0 == 1) {
            > do_error("Oh dearie me: %d %d %d\n", 0, 1, 0==1);
            > }
            >
            > return 0;
            > }
            >
            > /** This is the interesting bit. */
            >
            > void do_error(const char *fmat, ...)
            > {
            > va_list ap;
            > printf("%s: ", Argv0);
            > va_start(ap, fmat);
            > vprintf(fmat, ap);
            > va_end(ap);
            > exit(EXIT_FAILU RE);
            > }
            >
            >
            > In case you can't figure it out from the code, the do_error function
            > takes a variable number of arguments, just like 'printf', but before
            > printing them, it tries to print the name of the executing program,
            > and then, once everything is printed, it terminates the program
            > directly. This is for those "no recovery possible" sorts of situations,
            > which may or may not apply to your problem.[/color]

            I normally do something along the lines of the following coupled with
            returning an error code.

            #define ERROR_CONTINUE( s) \
            fprintf (stderr, "%s:%05i:%s \n", __FILE__, __LINE__, s)

            #define ERROR_FATAL(s,e ) \
            (fprintf (stderr, "FATAL ERROR*"), ERROR_CONTINUE( s), exit(e))
            [color=blue]
            >
            > As you may know, and unlike C++, C does *not* support exception handling
            > at the language level. (Anyone got a nice throw/catch sort of idiom for
            > standard C? :)
            >[/color]

            I've done a simplistic try/catch/throw with C using macros
            extensively, but imho it looks very ugly indeed (although it
            works as expected). i also toyed with the idea of doing an
            error reporting mechanism via a few macros and a signal handling
            function, along the lines of

            #define THROW(e) raise(e)

            this of course needs no try/catch statement, the signal handling
            function has to take care of the errors. i found this to be unsuitable
            as well, especially when i need to close files or free memory before
            ending.

            my current method is to write my code as a state machine using
            function pointers:

            struct error_t {
            char *file;
            int line;
            char *mesg;
            };

            struct args_t {
            ...
            };

            typedef int (*fp) (struct error_t *, struct args_t *);

            ....
            int main (void) {
            struct error_t errors;
            struct args_t args;
            fp function_to_run ;
            ...
            while (function_to_ru n!=NULL) {
            if (function_to_ru n (&errors, &args)) {
            /* handle error here */;
            }
            }
            ...
            }

            unfortunately, this does not lend itself all that well to lots of
            solutions, so i just stick with my first method above of reporting
            file and line number (and error) and then returning values indicating
            success or failure and letting the caller take care of it.

            goose,

            Comment

            • Fred L. Kleinschmidt

              #7
              Re: Anybody know how I would code an error statement when a user inputsan invalid input.



              jeff regoord wrote:[color=blue]
              >
              > A user inputs a float value. The scanf() function gets the value.
              > However, I need to create an error handler with an if else statement
              > saying invalid input if the input is not a number. Does anybody know
              > how I could do this?[/color]

              #include <stdio.h>
              int main( int argc, char *argv[] ) {
              char buffer[128];
              int n;
              double value;
              char ch[2];
              /* Should worry about entering string longer than buffer size,
              * but for simplicity, ignore for now...
              */
              while ( fgets( buffer, sizeof(buffer), stdin) ) {
              n = sscanf( buffer, "%lf%1s", &value, ch );
              if ( n == 1 ) {
              printf( "Number entered: %lf\n", value );
              }
              else {
              printf( "Invalid input\n");
              }
              }
              }
              --
              Fred L. Kleinschmidt
              Boeing Associate Technical Fellow
              Technical Architect, Common User Interface Services
              M/S 2R-94 (206)544-5225

              Comment

              • Mark McIntyre

                #8
                Re: Anybody know how I would code an error statement when a user inputs an invalid input.

                On 10 Sep 2003 19:05:39 -0700, in comp.lang.c , jhr71@netzero.c om
                (jeff regoord) wrote:
                [color=blue]
                >A user inputs a float value. The scanf() function gets the value.
                >However, I need to create an error handler with an if else statement
                >saying invalid input if the input is not a number. Does anybody know
                >how I could do this?[/color]

                easy - don't use scanf. Its a dangerous and hard to control function
                with limited error handling. Use fgets and either sscanf or strod.

                --
                Mark McIntyre
                CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>

                Comment

                • jeff regoord

                  #9
                  Re: Anybody know how I would code an error statement when a user inputs an invalid input.

                  Mark McIntyre <markmcintyre@s pamcop.net> wrote in message news:<07s1mv4fi ef9gv4qrj65ovqq sd9c2pe15h@4ax. com>...[color=blue]
                  > On 10 Sep 2003 19:05:39 -0700, in comp.lang.c , jhr71@netzero.c om
                  > (jeff regoord) wrote:
                  >[color=green]
                  > >A user inputs a float value. The scanf() function gets the value.
                  > >However, I need to create an error handler with an if else statement
                  > >saying invalid input if the input is not a number. Does anybody know
                  > >how I could do this?[/color]
                  >
                  > easy - don't use scanf. Its a dangerous and hard to control function
                  > with limited error handling. Use fgets and either sscanf or strod.[/color]


                  OK thank you all. Im a novice. How would I make an if else saying
                  invalid entry to this program. Maybe no number input
                  /* Currency Conversion. User inputs amount of Canadian dollars he or
                  she has. The program decides if */
                  /* valid entry was input by user. If valid entry occurs. Program
                  outputs conversion to U.S dollars */
                  /*If there is an invalid entry an error message is ouptut Not a valid
                  entry */

                  #include <stdio.h>


                  float cad = 0.0; /* CAD Canadian Dollar variable */
                  float usd = 0.0; /* USD U.S dollar variable */

                  int main() /* start main function */

                  {

                  printf( "\n \n ** Currency Conversion ** \n \n"); /* Print
                  name of program */

                  printf("Enter number of Canadian Dollars then press enter :
                  ");/* Print statement informing user to input number of canadian
                  dollars */
                  scanf("%f", &cad); /* gets the value entered by user */

                  if(cad < 0.0)
                  printf("Not a valid entry");
                  else
                  usd = cad * 0.725415; /* Canadian dollars conversion to
                  U.S dollars*/
                  printf(" \n Your Canadian dollars are equal to $
                  %.2f", usd); /* Output to screen U.S value */
                  printf(" in U.S dollars.\n \n");


                  return 0; /* Return statement to turn control over to operating
                  system */
                  }

                  Comment

                  • The Real OS/2 Guy

                    #10
                    Re: Anybody know how I would code an error statement when a user inputs an invalid input.

                    On Sat, 13 Sep 2003 00:03:36 UTC, jhr71@netzero.c om (jeff regoord)
                    wrote:
                    [color=blue]
                    > Mark McIntyre <markmcintyre@s pamcop.net> wrote in message news:<07s1mv4fi ef9gv4qrj65ovqq sd9c2pe15h@4ax. com>...[color=green]
                    > > On 10 Sep 2003 19:05:39 -0700, in comp.lang.c , jhr71@netzero.c om
                    > > (jeff regoord) wrote:
                    > >[color=darkred]
                    > > >A user inputs a float value. The scanf() function gets the value.
                    > > >However, I need to create an error handler with an if else statement
                    > > >saying invalid input if the input is not a number. Does anybody know
                    > > >how I could do this?[/color]
                    > >
                    > > easy - don't use scanf. Its a dangerous and hard to control function
                    > > with limited error handling. Use fgets and either sscanf or strod.[/color]
                    >
                    >
                    > OK thank you all. Im a novice. How would I make an if else saying
                    > invalid entry to this program. Maybe no number input
                    > /* Currency Conversion. User inputs amount of Canadian dollars he or
                    > she has. The program decides if */
                    > /* valid entry was input by user. If valid entry occurs. Program
                    > outputs conversion to U.S dollars */
                    > /*If there is an invalid entry an error message is ouptut Not a valid
                    > entry */
                    >
                    > #include <stdio.h>
                    >
                    >
                    > float cad = 0.0; /* CAD Canadian Dollar variable */
                    > float usd = 0.0; /* USD U.S dollar variable */[/color]

                    It's a bad idea to hold monetary data in float. Float is not precise
                    enough to hold currencies. What give 1.00 / 0.37 ?
                    See what your float will give you. See what you bank will say to it.

                    use long or long long instead.
                    [color=blue]
                    > int main() /* start main function */
                    >
                    > {
                    >
                    > printf( "\n \n ** Currency Conversion ** \n \n"); /* Print
                    > name of program */
                    >
                    > printf("Enter number of Canadian Dollars then press enter :
                    > ");/* Print statement informing user to input number of canadian
                    > dollars */
                    > scanf("%f", &cad); /* gets the value entered by user */[/color]

                    I type abde
                    scanf returns 0 Your program calculates with the last input prior.

                    I type -1.3456789
                    scanf returns 1 but your program cries invalid number.

                    I type e4567

                    It will be accepted, even as the input should be 34567 and the user
                    had made a typing mistake.

                    I type 0.0000000004567 89
                    your program accepts it, even as a currency has only 2 decimal digits.

                    What occures with the input 1e3.47b2?

                    You have to do pernickety checking the input. None of the functions in
                    the standard C library is able to do that.
                    As you needs to check the input precise you should use getc() to read
                    the input and convert on the fly. The converted result will be fine
                    only when the whole line is guilty, else remove anything from the
                    input stream until '\n' is found and give an error message to the
                    user.

                    Use long or long long, calculate cents, not dollars, print out
                    dollars.cents
                    [color=blue]
                    > if(cad < 0.0)
                    > printf("Not a valid entry");
                    > else
                    > usd = cad * 0.725415; /* Canadian dollars conversion to
                    > U.S dollars*/
                    > printf(" \n Your Canadian dollars are equal to $
                    > %.2f", usd); /* Output to screen U.S value */
                    > printf(" in U.S dollars.\n \n");[/color]

                    How precise is the result when someone type 17131713.13?

                    Gets you USD 0.0943039 when you tries to convert 0.13 CSD? Should you
                    not print 0.09?

                    Is 0.1235342274543 357e37 a guilty currency member?

                    You have some work to print a value that is formatted like a currency
                    and not ony a mathematical value. But anyway the result will be really
                    worse when you lets a float or double falsyfy the result.

                    Do never trust a float/double when you have to calculate commercial.

                    --
                    Tschau/Bye
                    Herbert

                    eComStation 1.1 Deutsch Beta ist verügbar

                    Comment

                    • Irrwahn Grausewitz

                      #11
                      Re: [OT] Anybody know how I would code an error statement when a user inputs an invalid input.

                      "The Real OS/2 Guy" <os2guy@pc-rosenau.de> wrote:
                      [color=blue]
                      > [...] The converted result will be fine
                      >only when the whole line is guilty,[/color]

                      This line is guilty of being Germish (or is it Engman?).

                      :-)))


                      --
                      do not write: void main(...)
                      do not use gets()
                      do not cast the return value of malloc()
                      do not fflush( stdin )
                      read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html

                      Comment

                      Working...