Math 171 Spring 2005, Lab 11 (double lab)

Due Thursday, March 10, 11:59 P.M.

  1. Overview
  2. Assignment
  3. Hints
  4. Extra Credit

Overview

The purpose of this lab is to introduce you to file processing and formatted I/O in Fortran.

Top

Assignment

Write a Fortran program called calc_interest that does the following:

  1. Prompts the user for a file from which to read information about credit card accounts.
  2. Saves this file to a backup.
  3. Calculates the monthly interest on each account, and modifies the balance accordingly.
  4. Overwrites the new information to the original filename.

If you encounter an error in the input file (as described below), you should restore your backup to the original filename, and then terminate program execution. The credit card information file has the following format:

The bulk of your work for this lab will be detecting syntax and data errors in the input file. Your program should detect the following errors:

See this reference on credit card numbers for help determining what constitutes a valid check digit, prefix, and length for each credit card type.

Top

Hints

  1. To start out, just make a prompt for a filename, and use a character variable to copy the contents of this file to standard output if it exists. If the file does not exist or cannot be opened, continue looping until a valid filename is found.
  2. Now add some code to move everything to a backup file. You may either modify the loop you wrote above, or insert the following code into your program:
    
        USE ifport ! this line goes after your PROGRAM statement and before your IMPLICIT none statement
    
        ...
    
        RENAME(oldname, newname) ! this line goes wherever you want the backup to occur
    
    
    where oldname and newname are character strings containing the old and new filenames. This has exactly the same effect as the UNIX shell command
    
        mv oldname newname
    
    
    You will want to test that the RENAME function returns a value of zero to make sure it completes successfully. The purpose of the USE command is tell the compiler you want to include the ifport module, which provides a definition of RENAME.
  3. Now go back and check whether each line in your character variable is a card type specification line, and reject any invalid card types. Your code should ignore any other lines at this point.
  4. Add some code that attempts to use a format to read in any lines you were ignoring in the previous step. You will need a backspace statement, a formatted input statement using nonadvancing I/O, another backspace statement, and another formatted input statement using advancing I/O. Use the return value of the first input statement to detect any errors in the formatting of each line. After reading in each line, write them out again using the same format. Note that since credit card numbers may be up to 16 digits long and integers can only hold numbers up to 2**31=2147483648, you will need to store the credit card numbers in a character variable. The other data can all be stored in variables of the appropriate type. Although it is possible to tell the compiler to use 64-bit integers (which can hold numbers up to 2**63=9223372036854775808, i.e. 18 arbitrary digits) using the "-integer_size 64" flag, this solution is unsatisfactory since
  5. Now calculate the interest and modify the balances.
  6. Now add some code to check whether credit cards contain non-numeric data.
  7. Add check digit support. This will require you to convert the character representation of the digits of the credit card number to integers. The ACHAR and IACHAR functions will help you here. Remember that your textbook has a copy of the ASCII collating sequence, which is also available using the command man ascii. You should assume the last digit is the check digit.
  8. Add checking for the card type-dependent errors, i.e. invalid credit card number lengths and prefixes.
  9. Add tests for the remaining data errors. Make sure the apr is positive, interest is calculated only for positive balance, and check for valid dates. Be sure your date test works correctly for leap years.
  10. Try out your program on these Sample Input Files
  11. Test some more to make sure your program catches any other errors not included in one of the input files I provided you with.

Top

Extra Credit

For an additional 15 points, write a program called ccgen that generates random, error-free input files for your interest calculator.

Top

Back to the course information page