CS 1713 Section 1, Summer 1997
Assignment 4: Loops and Files: money.c

Suppose you work at a foreign currency exchange desk in an airport or bank in the United States. You need to keep track of the current exchange rates for currencies and the amount of each currency you currently have on hand. You also need to be able to process transactions as customers come to buy or sell banknotes. Customers may buy foreign currencies with US Dollars

100 Dollar Bill

or sell foreign currencies such as German Marks

10 Deustche Mark Note

French Francs

50 Franc Note

or British Pounds

20 Pounds Sterling Note
for their equivalent in US Dollars.

Read this assignment completely and carefully; there are hints on how to read the input and rules on how the input and output must look.

For this assignment, you will write a C program that handles this kind of business. The program should read a file called exchange-info (available on runner in the directory ~djimenez/cs1713) to determine the exchange rate and current available balance for each currency. The file looks like this:
D       0.579240        525.00
F       0.171718        1213.00
B       1.664300        986.00
U       1.000000        10000.00
The currencies are specified by D, F, B, or U for German, French, British, and United States, respectively. The exchange rates follow the country code and are in terms of US Dollars so, e.g., it costs 1.6643 Dollars to purchase one British Pound, 57.924 cents to buy one German Mark, and so forth. The final number is the amount of currency available so at the beginning of the program we have e.g. 525 German Marks, 10000 Dollars, and so forth.

Note: You must not hardwire these values into your program, because they may change by the time you turn in the assignment; your program must read them. Currency exchange rates can be very volatile, so your program must be able to handle day-to-day changes by reading in the file.

After you have read the exchange rates file, your program should begin processing transactions read from the standard input (i.e., keyboard or redirected file using scanf) until end of file, then print the final balance for each currency. A transaction is the letter b or s (for buy or sell, resp.) followed by a currency code (D, F, or B) followed by an amount of that currency; all of these items should be read in from the same line in the same scanf statement; do not prompt for input in between items.

For buy transactions, the customer is buying foreign banknotes from the bank with US Dollars. For sell transactions, the customer is selling foreign banknotes to the bank for US Dollars. The customer may not buy Dollars or sell for other than Dollars. Don't worry about fractional money; it is assumed you have enough foreign coins on hand to handle this.

A transaction will be successful unless the customer tries to buy too much currency or sell currency worth more than the Dollars on hand. After each transaction, your program should print the type of transaction and the amounts involved, or an error message.

Here is sample output where the customer buys 100 German Marks:
User input:
b D 100
Program output:
customer buying 100.00 German Marks, that will be $57.92, please.
Here is sample output where the customer sells 123.56 French Francs:
User input:
s F 123.56
Program output:
customer selling 123.56 French Francs, here is $21.22
Here is sample output where the customer tries to sell more British Pounds than the bank can afford, since the bank doesn't have enough Dollars (this is a small bank).
User input:
s B 10000
Program output:
not enough dollars, sorry.
And here is sample output at the end of the program (signified on the keyboard with CTRL-D for end of input), assuming the above transactions:
Ending Balances:
Dollars:        10036.71
German Marks:   425.00
French Francs:  1336.56
British Pounds: 986.00
On runner in ~djimenez/bin, the instructor's version of the money program can be found. Run it to see how yours compares. Your program needs to work just like this program to get full credit; don't put any extraneous prompts or informative messages in your program. Like the previous Automata program, your program will be checked automatically by another program. This program does not need any command line parameters.

For reading in the exchange rate information, the following code (assuming FILE pointer f and other variable declarations) will work until feof(f):
fscanf (f, "%c %lf %lf\n", &unit, &rate, &amount);
For reading from the keyboard or other standard input, the following will work:
scanf ("%c %c %lf", &command, &unit, &Amount);
but you must do a subsequent scanf ("\n"); so the next character can be read properly.

In your weekly progress report due Friday night, report on your experiences writing the program. Give examples of the output of your program.

This assignment is due at midnight on Friday, July 11, 1997.