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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
using namespace std;
int const MAX_SIZE = 1000;
struct Student
{
string studentID;
string firstName;
char midInitial;
string lastName;
/**
* displays the menu for the class roster program
*/
void menu()
{
cout<<" CLASS ROSTER APPLICATION"<<endl;
cout<<"========================================="<<endl;
cout<<"ADD a student record..................[A]"<<endl;
cout<<"DELETE a student......................[D]"<<endl;
cout<<"EDIT a student record.................[E]"<<endl;
cout<<"INQUIRY about a student...............[I]"<<endl;
cout<<"LIST all students.....................[L]"<<endl;
cout<<"QUIT the system.......................[Q]"<<endl;
}
/**
* does a linear search for a specified student ID #
* @param roster an array of student records
* @param numStuds the number of students (class size)
* @param sID a student ID number
* @return the subscript/index at which the record with
* the specifed student ID can be found on the class
* roster or -1 if it cannot be found
*/
int linearSearch(const Student roster[], int numStuds, string sID)
{
int i;
for (i=0; i<numStuds; i++)
{
if (roster[i].studentID == sID)
return i;
}
return -1;
}
/**
* sort the class roster by student ID # using selection sort
* @param roster an array of student records
* @param numStuds the number of students (class size)
*/
void selSort(Student roster[], int numStuds)
{
int minIDPos;
Student tempStudRecord;
int i,j;
for (i=0; i<numStuds-1; i++)
{
minIDPos = i;
for (j=i+1; j<numStuds; j++)
{
if (roster[j].studentID < roster[minIDPos].studentID)
minIDPos = j;
}
tempStudRecord = roster[i];
roster[i] = roster[minIDPos];
roster[minIDPos] = tempStudRecord;
}
}
/**
* adds a new Student record as the last record in the roster
* array when its studentID field is not already in the array or
* displays a message indicating that the student ID # has already
* been assigned
* @param roster an array of student records
* @param numStuds the number of students (class size)
* @param newStudent the new student's record
*/
void addStudent(Student roster[], int& numStuds, Student newStudent)
{
int pos = linearSearch(roster,numStuds,newStudent.studentID);
if (pos == -1)
roster[numStuds++] = newStudent;
else
cout<<newStudent.studentID<<" has already been assigned. "<<endl;
}
/**
* removes a student record from the roster array when the specified
* studentID field is in the array, moves all succeeding student
* records up by one position (index) and decrement the number of
* students by 1; display a message indicating that the student ID
* has not been assigned if studentID field is not in the roster array
* @param roster an array of student records
* @param numStuds the number of students (class size)
* @param sID the student's ID #
*/
void deleteStudent(Student roster[], int& numStuds, string sID)
{
int pos = linearSearch(roster,numStuds,sID);
if (pos == -1)
cout<<sID<<" does not exist in this record. "<<endl;
}
/**
* edits a Student record given the specified student ID when
* the roster array contains the ID; display a message indicating
* that the student ID has not been assigned if studentID field is
* not in the roster array
* @param roster an array of student records
* @param numStuds the number of students (class size)
* @param sID the student's ID #
*/
void editStudent(Student roster[], int numStuds, string sID)
{
}
/**
* display a Student record given the specified student ID when
* the roster array contains the ID; display a message indicating
* that the student ID has not been assigned if studentID field is
* not in the roster array
* @param roster an array of student records
* @param numStuds the number of students (class size)
* @param sID the student's ID #
*/
void studentInquiry(const Student roster[], int numStuds, string sID)
{
/** 5. implement this function **/
}
/**
* display the class roster when there is at least one student
* or prints a message indicating that there are currently no
* students enrolled.
* @param roster an array of student records
* @param numStuds the number of students (class size)
*/
void listStudents(const Student roster[], int numStuds)
{
cout<<"C L A S S R O S T E R"<<endl;
cout<<"Student ID # First Name M.I. Last Name"<<endl;
cout<<"---------------------------------------------"<<endl;
int i=0;
while (i<numStuds)
{
cout<<left;
cout<<setw(14)<<roster[i].studentID<<setw(13)<<roster[i].firstName;
cout<<roster[i].midInitial<<". "<<roster[i].lastName;
cout<<endl;
i++;
}
cout<<right;
cout<<"---------------------------------------------"<<endl;
cout<<"Enrollment: "<<i<<endl;
}
int main()
{
Student roster[MAX_SIZE];
fstream inFile;
fstream outFile;
string filename, sID;
Student newStudent;
int numStuds = 0;
char choice;
int i;
cout<<"Enter the name of the data file> ";
cin>>filename;
inFile;
inFile.open("roster.txt", ios::in);
if (!inFile)
cout<<"the file did not open"<<endl;
while (numStuds < MAX_SIZE && inFile >> roster[numStuds].studentID >>
roster[numStuds].firstName >> roster[numStuds].midInitial >>
roster[numStuds].lastName)
{
numStuds++;
}
do
{
cout<<endl;
menu();
cout<<endl;
cout<<"Select an option-> ";
cin>>choice;
switch(toupper(choice))
{
case 'A': cout<<"Enter the student ID #> ";
cin>>newStudent.studentID;
cout<<"Enter the student's first name> ";
cin>>newStudent.firstName;
cout<<"Enter the student's middle initial> ";
cin>>newStudent.midInitial;
cout<<"Enter the student's last name> ";
cin>>newStudent.lastName;
addStudent(roster,numStuds,newStudent);
break;
case 'D': cout<<"Enter the student ID Number: ";
cin>>sID;
deleteStudent(roster, numStuds, sID);
break;
case 'E': cout<<"Enter the student ID Number: ";
cin>>sID;
editStudent(roster, numStuds, sID);
break;
case 'I': cout<<"Enter the student ID Number: ";
cin>>sID;
studentInquiry(roster, numStuds, sID);
break;
case 'L': selSort(roster, numStuds);
listStudents(roster, numStuds);
break;
case 'Q': break;
default: cout<<"Invalid menu choice...try again!"<<endl;
}
}
while(toupper(choice) != 'Q');
{
outFile.open(filename.c_str(), ios::out);
if (!outFile)
{
cout<<"Unable to open "<<filename<<" for output. "<<endl;
return -1;
}
}
for (i=0; i<numStuds;i++)
{
outFile<<roster[i].studentID<<" "<<roster[i].firstName<<" ";
outFile<<roster[i].midInitial<<" "<<roster[i].lastName<<endl;
}
outFile.close();
return 0;
}
|