Thread: help with error checking

  1. #1
    God
    Join Date
    Nov 2005
    Location
    Ireland
    Posts
    2

    help with error checking

    Hi i'm doing a project for college thats an cash register style program that lets you choose ither euro or sterling to make ur transaction in anyways i had no problem writing the program at all but we have to have the program to error check its self and i came across a very weird error that i can't think of a way to fix i'd aperiate any hints on how to fix it thank you


    Code:
    while ( num != 0 )
    {
      printf( "Please enter product price: " ) ;
      scanf( "%f" , &num ) ;
      total += num ;
      printf( "The total price so far is %5.2f\n\n\n" , total ) ;
    }
    total and num are float vars but the error is when i type in a letter it will continuiously add 1 pound on every second how would i stop this from happening like displaying an error message is someone types in an letter? thanks for the help.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    scanf() returns
    The number of items succesfully read. This count doesn't include any ignored fields with asterisks (*).
    If EOF is returned an error has occurred before the first assignation could be done.
    So, test scanf() like so:
    Code:
    if(scanf("%f" , &num) != 1) {
        clearerr(stdin);
        /* do something about error */
    }
    Last edited by dwks; 11-02-2005 at 03:22 PM. Reason: added clearerr
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    God
    Join Date
    Nov 2005
    Location
    Ireland
    Posts
    2
    Oh Alright thanx alot i shall give it a try cheers

Popular pages Recent additions subscribe to a feed