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.
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