Saturday, May 22, 2010

How do I make this 'c' (not 'c++') code return an error message if an alpha is inputed ?

#include %26lt;stdio.h%26gt;


int main(void)





{ float length, width;








printf("please enter length: ");


scanf("%f", %26amp;length);





printf("please enter width);


scanf("%f", %26amp;width);








printf(" this is your length: \n", length);





printf(" this is your width: \n", width);





return(0);





}

How do I make this 'c' (not 'c++') code return an error message if an alpha is inputed ?
Check the return value of scanf.





These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.





The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
Reply:look up the isalpha() function





if (isalpha(mychar))


{


printf("error, invalid input");


exit(0)


}
Reply:Don't use


int main(void)


just use


int main()

daffodil

No comments:

Post a Comment