Lab 9 - Structures

First, copy struct_example.cpp to your Helios account using the command:
cp /usr/users/mdanfor/public_html/cs221-f06/struct_example.cpp .
Compile and run the example. The program is similar to the examples given in lecture on Wednesday. It defines a StudentRecord structure that contains a name, student ID number and phone number. In main, it creates an array of 50 StudentRecords, which means up to 50 names, IDs and phone numbers can be used by this program. The get_input() function prompts for how many students to read in and then calls new_student() to create the record for each student.

Lab Assignment

Modify get_input() and new_student() to read the student information from a file instead of using the keyboard. You can either define a get_file_input() function for file input if you wish to preserve the original keyboard version of get_input() or you can remove the original get_input() and replace it with the file version. The sample input files are: These files are in what is called a tab seperated format. There is a tab between the name and student ID. There is a second tab between the ID and phone number. There is a space between the first name and last name. Your input function should use this code to open the file:
ifstream fin;
char filename[30];

printf("Enter filename: ");
cin >> filename;
fin.open(filename);

Hint: getline() actually has two forms. There is

getline(str, len)
which gets input up until the end of line ('\n') is seen. The second form is
getline(str, len, delimiter)
where delimiter is the character to use to end the input, such as the tab.

Hint 2: Modify new_student() to take an input stream and a character for the delimiter as a parameter. See the clear_buffer() function to see how to pass an input stream to a function. Then you can use the same new_student() function for both file input and keyboard input. The function calls would look like:

    s[i] = new_student(cin, '\n'); // keyboard input
    s[i] = new_student(fin, '\t'); // file input