c - Currency conversion database structure from text file -
i've been working on program data structure reads in file of different currencies, call use conversion. i've been running on days on end , tried fgets
, fscanfs
, , such, , i'm lost @ point pretty new programming.
the dat. file outlined on separate lines this:
dollar 1.00 yen 0.0078 franc 0.20 mark 0.68 pound 1.96
my code far:
typedef struct { string currency; double rate; } currencyt; typedef struct { currencyt cur[maxcurrencytypes]; int ncurrency; } *currencydb; static currencydb readdatabase(void); static readoneline(file *infile, currencydb db); static currencydb readdatabase(void) { file *infile; currencydb db; int ncurrency; db = new(currencydb); infile = fopen(exchangefile, "r"); while (readoneline(infile, db)); fclose(infile); return(db); } static readoneline(file *infile, currencydb db) { currencyt cur; char termch, currency; double rate; int nscan, ncurrency; ncurrency = 0; while(1) { nscan = fscanf(infile, "%20s %f%c", db->cur[ncurrency].currency, &db->cur[ncurrency].rate, &termch); if(nscan = eof) break; if(nscan != 3 || termch != '\n') { error("improper file format"); } ncurrency++; } db->ncurrency = ncurrency; } static void processexchange(currencydb db) { } main() { currencydb currencies; currencies = readdatabase(); processexchange(currencies); }
see 8 & 9 first.
maxcurrencytypes
undeclared, let's assume#define maxcurrencytypes (5)
string
undeclared, let's assumetypedef char * string;
currencydb
pointer type, recommend instead declaring structure typetypedef struct { currencyt cur[maxcurrencytypes]; int ncurrency; } currencydb; // drop *
static readoneline()
should use explicit return type.static int readoneline()
int ncurrency;
not used inreaddatabase()
.new()
indb = new(currencydb)
not declared nor defined. assume allocate uninitialized memory*db
.exchangefile
not declared.if (nscan = eof)
->if (nscan == eof)
readoneline()
needs return value.main()
should explicitly state return type , parameters.int main(void)
Comments
Post a Comment