ANSI C: Error checking user input with a function -


i attempting use single error checking function check errors within multiple other functions. however, when running program, gets stuck in loop , won't break it. once application gets valid input, should continue next function. repeats fstartbalance function if error checking returns 0. i'm kind of beginner, i'm not @ troubleshooting. here applicable code:

/* begin fvalidatefloat */ int fvalidatefloat(float input, float minvalue, float maxvalue) {     int failed = 0;      while (input < minvalue)     {         printf("\nerror: input low, please enter value greater or equal %.2f.", minvalue);         failed = 1;         return failed;     }     while (input > maxvalue)     {         printf("\nerror: input high, please enter value less or equal %.2f.", maxvalue);         failed = 1;         return failed;     } } /* end fvalidatefloat */  /* begin fstartbalance */ float fstartbalance(void) {     float startbalance; /* declare variable */     int failed;      while (failed = 1)     {         printf("\n\nnow enter current balance in dollars , cents: "); /* prompt user input current balance */         scanf("%f", &startbalance);         fflush(stdin);         failed = fvalidatefloat(startbalance, 0, 3.4e+38);     }      return startbalance; /* return variable */ } /* end fstartbalance */ 

thanks help.

fflush(stdin); undefined

you never return value fvalidatefloat() in case while statements not taken:

        return failed;     } } /* end fvalidatefloat */ 

in same function while loops don't make sense, use if statement instead:

while (input < minvalue) {     printf("\nerror: input low, please enter value grea%.2f.", minvalue);     failed = 1;     return failed; } 

you setting failed 1 in while loop, instead of comparing 1:

while (failed = 1) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -