Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
How can I improve these functions?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Malcolm, post: 1705263"] /* First I'll give you local improvements, then I'll explain how to redesign. */ /* You want to check that a full line was read by looking for the newline. */ sscanf() returns the number of items successfully converted. Check the return value and bale if the record is malformed. /* This is a bit lame. You need to be able to read records until the computer runs out of memory. */ /* You might consider printing errors to stderr. */ Why not rewrite the function /* Returns an allocated array of N drinks read from the file. */ DRINK *readdrinks(char *fname, int *N) { } Also, you could get rid of all the output to printf() and put it somewhere else, so the function is more portable. Write the function int validmoney(const char *str, double minprice, double maxprice) { Call strtod. Check for garbage following what you read. This tells you you have a valid floating-point number. Checking for min price and max price is trivial (min might be 1 cent, max can be DBL_MAX or you might want to sanity check). However 12 and 12.34 are valid, 12.3 and 12.345 are not. You need to use strchr() to seach for the decimal point and, if it exists, count the digits (using isdigit). If there are two then you are OK, if not then reject it. } Input can also be invlaid if fgets() fails to read a line. keep this separate from your validmoney() function. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
How can I improve these functions?
Top