! ! This program written by Clint Whaley for CS 1073 assg#3, ! due Tuesday, 10/20/09. No collaborators. ! PROGRAM filetab ! ! This program takes in input from the keyboard describing some sales, ! and prints out a formatted table to the file receipt.txt. ! IMPLICIT NONE CHARACTER(LEN=8) :: name CHARACTER(LEN=40) :: desc DOUBLE PRECISION :: price, cost, totprice, totcost ! ! Open the output file and print the column headers/heading markers to it ! OPEN(unit=10, action='write', file='receipt.txt', status='replace') WRITE (10, 1000) WRITE (10, 2000) ! ! Start price and cost totals at zero, and read records from keyboard ! totprice = 0.0 totcost = 0.0 DO ! ! Prompt, read in, and error check cost ! PRINT*, 'Enter cost' READ *, cost IF (cost .lt. 0.0) EXIT ! neg # means done with input IF (cost .eq. 0.0) THEN ! Zero cost illegal by prob def WRITE (*,2200) WRITE (10,2200) CLOSE (10) STOP ENDIF ! ! Prompt, read in, and error check price ! PRINT*, 'Enter price' READ *, price IF (price .lt. cost) THEN ! price must be less than cost WRITE (*, 2400) cost, price WRITE (10, 2400) cost, price CLOSE (10) STOP ENDIF IF (price .gt. cost+cost) THEN ! price cannot be more than twice cost WRITE(10, 2600) price, cost+cost WRITE(*, 2600) price, cost+cost CLOSE(10) STOP END IF ! ! Prompt and read in anme and description ! PRINT*, 'Enter name' READ *, name print *, 'NAME = ', name PRINT*, 'Enter description' READ *, desc print *, 'DESC = ', desc ! ! Write line to table, increment running total price and cost ! WRITE (10, 3000) name, desc, cost, price, price-cost totprice = totprice + price totcost = totcost + cost END DO ! ! Write out required footer info; total profit is simply totprice - totcost ! WRITE (10,*) ' ' WRITE (10, 4000) 'COST', totcost WRITE (10, 4000) 'PROFIT', totprice - totcost CLOSE (10) 1000 FORMAT (' NAME DESCRIPTION COST& & PRICE PROFIT') 2000 FORMAT ('======== ======================================== =======& & ======== ========') 2200 FORMAT ('FATAL ERROR: COST must be greater than zero') 2400 FORMAT ('FATAL ERROR: PRICE must be greater than or equal to ', F7.2, & ' but you entered: ', F8.2) 2600 FORMAT ('FATAL ERROR: PRICE is ', F7.2, ' but should be <= ', F7.2) 3000 FORMAT (a8, 2x, a40, 2x, F7.2, 2x, F8.2, 2x, F8.2) 4000 FORMAT ('TOTAL ', a6, ' = ', F10.2) END PROGRAM filetab