1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
# include <iostream>
# include <cstring>
# include <fstream>
# include <cctype>
# include <cstdlib>
# include <cstdio>
// structure definition
struct student
{
char firstName[21]; // first name of student
char middleIn; // middle initial of student
char lastName[21]; // last name of student
char SSN[10]; // social security number of student
struct student * next; // pointer to link structures
};
// function prototypes
int readFile( struct student *, char *, int ); // opens input file and reads info into structures
void searchName( struct student * ); // searchs through structures and prints out records that have last
// name entered by user
void searchNum( struct student * ); // searchs through structures for a record that has a SSN number that
// the user has entered
void printList( struct student *, int ); // prints entire list of student structures
void destroyList( struct student * ); // destroys space when program quits
using namespace std;
int main( int argc, char * argv [] )
{
char fileName[20]; // name of file
struct student * record; // structure of strudent info
int numStudents; // number of student structures created from input file
int choice; // option chosen by user
// see if filename is in argv
if( argc == 0 )
{
cerr << "No file name included\n";
exit( EXIT_FAILURE ); // end program after printing error
}
else
//call function to read from file
strcpy( fileName, argv[1] );
readFile( record, fileName, numStudents );
// print user interface
cout << "1. Search for a Student by Last Name\n";
cout << "2. Search for a Student by SSN number\n";
cout << "3. Print full list of Students\n";
cout << "4. Quit\n";
cin >> choice;
while( choice != 4 )
{
if( choice == 1 )
{
searchName( record );
}
else if( choice == 2 )
{
searchNum( record );
}
else if( choice == 3 )
{
printList( record, numStudents );
}
else
cerr << "Invalid Choice\n";
exit( EXIT_FAILURE );
// pause program
cout << "Press ENTER to continue";
cin.get ( );
cin.ignore ( 1, '\n' );
}
destroyList( record );
return 0;
}
int readFile( struct student * start, char fileName[], int numStudents )
{
ifstream input; // input stream used
struct student * current; // where parser is located
struct student * newrecord; // pointers for future use
char buffer[81]; // string that is read from file
int count; // count for loop
int length; // length of line read from file
// open input file
input.open( fileName );
if( !input.is_open() )
{
cerr << "Unable to open input file:" << fileName << "\n";
exit( 1 );
}
numStudents= 0;
start = NULL;
// do a priming read
input.getline( buffer, 81 );
while( !input.eof() )
{
length = strlen( buffer );
for( count = 0; count < length; count++ )
{
if( strncasecmp( buffer, "<STUDENT>", length ) )
{
newrecord = new struct student;
if( newrecord )
{
newrecord -> next = NULL;
}
}
else if( strncasecmp( buffer, "<FIRST>", length ) )
{
input.getline( buffer, 81 );
while( length == 0 )
{
input.getline( buffer, 81 );
}
strcpy( newrecord -> firstName, buffer );
}
else if( strncasecmp( buffer, "<MI>", length ) )
{
input.getline( buffer, 81 );
while( length == 0 )
{
input.getline( buffer, 81 );
}
newrecord -> middleIn = * buffer;
}
else if( strncasecmp( buffer, "<LAST>", length ) )
{
input.getline( buffer, 81 );
while( length == 0 )
{
input.getline( buffer, 81 );
}
strcpy( newrecord -> lastName, buffer );
}
else if( strncasecmp( buffer, "<SSN>", length ) )
{
input.getline( buffer, 81 );
while( length == 0 )
{
input.getline( buffer, 81 );
}
strcpy( newrecord -> SSN, buffer );
}
else if( strncasecmp( buffer, "</STUDENT>", length ) )
{
numStudents++;
if( start == NULL )
{
start = current = newrecord;
}
else
current -> next = newrecord;
current = current -> next;
}
input.getline( buffer, 81 );
}
}
input.close();
return numStudents;
}
|