c - Making a validation function to tell if the number entered is positve -


/* function i'm trying make bigger program calcs area of room. when answer <= 0 want function take over, keep getting few arguments error when compile it. gladly appreciated.*/

#include <stdio.h> int validinput(double len); int main(void) { double len ; int check; printf("\nenter length of room in feet:");         scanf("%lf", &len); check = validinput();  return 0;  }  int validinput(double len) { int check; if (len <= 0 ) printf("\nnumbers entered must positive."); return check; } 

this line:

check = validinput(); 

is missing parameter. suggest:

check = validinput( len ); 

of course, code should check returned value, not parameter value, call scanf() assure operation successful. in case, returned value should 1. other returned value indicate error occurred.

regarding validinput() function:

the variable check never initialized specific value. suggest:

int validinput(double len) {     int check = 1; // indicate valid      if (len <= 0 )     {         check = 0; // indicate not valid         printf("\nnumbers entered must positive.\n");     }      return check; } 

note: trailing \n on call printf() force text output rather setting in internal stdout buffer until program exits, @ time output.

incorporating appropriate error checking main() function results in:

#include <stdio.h>   // scanf(), printf() #include <stdlib.h>  // exit(), exit_failure  int validinput(double len);  int main(void) {     double len ;     int check;      printf("\nenter length of room in feet:");     if( 1 != scanf("%lf", &len) )     { // scanf failed         perror( "scanf length of room, in feet, failed" );         exit( exit_failure );     }      // implied else, scanf successful      check = validinput( len );      printf( "the user input %s\n", (check)? "valid" : "not valid");      return 0;  } // end function: main 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -