# CHARACTER Input

### Problem Statement

This problem was discussed in a previous page using the I descriptor. Since the problem does not involve any computation, we shall redo it using the A descriptor.

Suppose we have an input like the following. Each row starts with a salesperson's number (10 positions), followed by his/her phone numbers (10 positions), followed by the bonus this person has earned. Each salesperson's number has seven digits, the first two gives the division code, followed by two digits of department code, followed by three digits of person code. A phone number has seven digits in the form of 482-0911.

```         1    1    2    2
....5....0....5....0....5
7645094   7079173   1000
8745363   6347862   1120
8701001   7130067   750
4000023   6700175   1253
8801810   9000018   980
```
Write a Fortran program that reads in the above input and prints the following table:
```         1    1    2    2    3    3
....5....0....5....0....5....0....5
Sales Amount Table
==================

Sales No.   Phone No.   Amount
---------   ---------   ------
76-45-094   707-9173      1000
87-45-363   634-7862      1120
87-01-001   713-0067       750
40-00-023   670-0175      1253
88-01-810   900-0018       980

Total  5 person(s) processed.
```
Note that the first position is always for printer control. Note also that we do not know the number of salespersons.

### Solution

```PROGRAM  Decoding
IMPLICIT  NONE
CHARACTER(LEN=2) :: SN_1, SN_2, SN_3*3
CHARACTER(LEN=3) :: Phone_1, Phone_2*4
CHARACTER(LEN=3) :: Filler_1, Filler_2
INTEGER          :: Amount
INTEGER          :: Number
INTEGER          :: Status
LOGICAL          :: Problem

WRITE(*,"(A,A)")  " ", "      Sales Amount Table"
WRITE(*,"(A,A)")  " ", "      =================="
WRITE(*,*)
WRITE(*,"(A, A)")  " ", "Sales No.   Phone No.   Amount"
WRITE(*,"(A, A)")  " ", "---------   ---------   ------"
Problem = .FALSE.
Number = 0
DO
READ(*,"(7A,I5)", IOSTAT=Status)  SN_1, SN_2, SN_3, Filler_1,  &
Phone_1, Phone_2, Filler_2, Amount
IF (Status > 0) THEN
WRITE(*,*)  "Something wrong in your input data"
Problem = .TRUE.
EXIT
ELSE IF (Status < 0) THEN
EXIT
ELSE
WRITE(*,"(10A,I10)")  &
" ", SN_1, "-", SN_2, "-", SN_3, Filler_1, &
Phone_1, "-", Phone_2, Amount
Number = Number + 1
END IF
END DO
IF (.NOT. Problem) THEN
WRITE(*,*)
WRITE(*,"(A,A,I3,A)")  " ", "Total", Number, " person(s) processed."
END IF
END PROGRAM  Decoding
```

### Program Input and Output

The output of the program is:
```         1    1    2    2    3    3
....5....0....5....0....5....0....5
Sales Amount Table
==================

Sales No.   Phone No.   Amount
---------   ---------   ------
76-45-094   707-9173      1000
87-45-363   634-7862      1120
87-01-001   713-0067       750
40-00-023   670-0175      1253
88-01-810   900-0018       980

Total  5 person(s) processed.
```

### Discussion

• The salesperson number can be read in with A2, A2 and A3, and the phone number can be read in with A3 and A4. As long as the length of the CHARACTER variables and the number of positions agree, the width of field values can be ignored. Therefore, SN_1 and SN_2 are declared to be CHARACTER variables of length 2, SN_3 and Phone_1 are declared with length 3, and Phone_2 are declared with length 4.
• But, there are spaces between fields. More precisely, there are three spaces between the salesperson number and the phone number, and another three spaces between the phone number and the bonus value. For now, we don't know how to skip these spaces. But, we can always read them in. Therefore, we declare two CHARACTER variables of length 3, Filler_1 and Filler_2, for reading in these two gaps.
• As a result, the variables are read with (7A,I5), where the first three As are for SN_1, SN_2 and SN_3, the next A is for reading three spaces into Filler_1, the next two As are for reading Phone_1 and Phone_2, the next A is for reading the three spaces into Filler_2, and finally I10 is for Amount.
• For the output, we need 10 As to print those strings, followed by I10 for printing Amount. Of these 10 As, the first one is for printing a space (i.e., printer control).