CSCI 240 - Assignment 11 - Spring 2007
Due: Friday April 27, 3 pm
Points: 100
Overview
For this assignment you will write two programs dealing with information about college applicants:
The input text file (download here) will contain sets of lines with information about applicants in the following format:
lastname firstname (max 39 chars each name)
id (9 chars)
gpa (a double)
score (an integer; represents either an ACT or SAT score)
the last line of the file (corresponding to a lastname) is "eof"
For example:
Woods Tiger 123456789 3.5 1200 ... more sets eof
The output file (of structs) should be named stinfo (with no file extension).
The structure should consist of the following members, in order:
last name (string up to 39 chars)
first name (string up to 39 chars)
id (string up to 9 chars)
gpa (a double)
score (an integer)
Note: if the following I/O topics have not been covered in lecture, read (in last (unnumbered) section of the Lecture Notes) the sections entitled
for information and examples of how to read and write text files and files of structs.
The File Writer Program
This program will read input, fill in a structure, and write the structure to disk - repeatedly until there is no more input. The information in each set of input lines (and thus in each structure) is shown above. You should define and create a single struct representing this information - but you should have separate fields in the struct for first and last names. Let's agree to call the struct name TStudent. This program only needs one TStudent struct. It can be "filled" with information, written to disk, and then "refilled", written again, etc. The names and SSN should be declared as char-array strings (be sure to provide enough space in the arrays for the terminating null).
The program should declare one ifstream variable for the input file. The first thing the program should do is to open the input file, checking for success and terminating the program with an error message if the file does not open. It will then read the first set of lines representing one student, build the struct, and then write it to disk. It will continue reading and building and writing until it reads a student last name of "eof". You will need to use strcmp() to test the last names that you read in your loop exit condition. You will have to devise the loop logic to repeatedly read from the input text file, build a structure, and write it out.
In order to write structs to disk, your program will also need to declare a ofstream variable for the output file, and open it. Again, check for success to be safe.
| Also - understand that the structures you write out cannot be correctly displayed by a word processor. The characters in the strings will show up, but so will a lot of garbage in the "unused" part of the char arrays. The numbers are written out in internal format, not as chars, so you won't be able to see them at all (except as junk). So you won't know if you have correctly saved all the information until you write the second (Structure Reader) program. |
You must close() the output file. It is also considered good practice to close() the input file, so do that, too.
The Structure Reader & Display Program
This program will read the file of structs written by the program described above. It will read the structs and store them into an array of structs (make it big enough to hold 100 such structs).
Briefly, this program will
Functions to accomplish the sub-tasks are given below. However, like all programs, it is suggested that you develop incrementally:
void dispStudent(TStudent student)
This simple function just displays one TStudent struct on two lines, approximately as follows:
Last: Woods First: Tiger ID: 123456789 GPA: 3.2 Score 1200
It will help to left-justify all fields. You can do this before outputting any display by coding
cout << setiosflags(ios::left);
void dispAllStudents(TStudent stAr[], int count)
This function uses dispStudent() to display all the TStudent structs in the array.
void sortByName(TStudent stAr[], int count)
This function uses the selection sort algorithm to sort the structs in the array by last name. Sort in acscending order (A to Z). Remember that to compare two strings, you need to use strcmp(). Also, when you swap two elements, remember that you are swapping the entire struct. This is much simpler than swapping every member one at a time. Just declare your temp variable as TStudent:
TStudent temp;
... temp = stAr[top]; etc.
Hand in
Electronically submit your programs as 11a (the write-structs program) and 11b (the read-and-display program).
Submit a copy of the both source code files in the drop box.
Code your programs so they read and write from the current directory (that is, no drive or path information should be in the fopen() statements).
Document each program so that it explains its role in the whole process, referring to the other program briefly.