- /********************************************************************
- * CSCI124 - Ass1
- * Gareth Woodorth - gw305
- * ass1.cpp - Contains function definitions for student records database
- ********************************************************************/
- #include <iostream>
- #include <ostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- // ============== Constants ==========================================
- const char cTextFileName[] = "ass1.txt";
- const char cBinaryFileName[] = "ass1.dat";
- const int cMaxRecs = 100;
- const int cMaxChars = 30;
- const int cMaxSubjects = 8;
- // ============= User Defined types ==================================
- enum StatusType{eEnrolled,eProvisional,eWithdrawn};
- struct SubjectType{ char Code[8]; StatusType Status; int Mark;};
- struct StudentRecord{
- long StudentNo;
- char FirstName[cMaxChars];
- char LastName[cMaxChars];
- int NumSubjects;
- SubjectType Subjects[cMaxSubjects];
- };
- // ============= Global Data =========================================
- StudentRecord gRecs[cMaxRecs];
- int gNumRecs=0;
- // ============= Private Function Prototypes =========================
- void PrintRecord(int i);
- int FindRecord(long StudentNum);
- // ============= Public Function Definitions =========================
- void ReadFile()
- {// Reads data file into array
- ifstream infile;
- infile.open (cTextFileName);
- if (infile.fail())
- {
- cerr << "Cant find data file!\n";
- exit(1);
- }
- int i = 0;
- infile >> gRecs[i].StudentNo;
- while(!infile.eof())
- {
- infile >> gRecs[i].FirstName;
- infile >> gRecs[i].LastName;
- infile >> gRecs[i].NumSubjects;
- int statustype;
- for ( int j=0; j < gRecs[i].NumSubjects; j++)
- {
- infile >> gRecs[i].Subjects[j].Code;
- infile >> statustype;
- gRecs[i].Subjects[j].Status = static_cast <StatusType> (statustype);
- infile >> gRecs[i].Subjects[j].Mark;
- }
- i++;
- infile >> gRecs[i].StudentNo;
- }
- gNumRecs = i;
- infile.close();
- cout << gNumRecs << " records read" << endl;
- }
- void DisplayRecord()
- {// Displays specified record on screen
- ifstream infile;
- int StudentNumber;
- cout << "Enter student number: ";
- cin >> StudentNumber;
- int i = FindRecord(StudentNumber);
- if (i==-1)
- cerr << "Record not found" << endl;
- else
- PrintRecord(i);
- }
- void SaveFile()
- {// Writes array to text data file
- ofstream ins;
- ins.open(cTextFileName);
- if (!ins.good())
- {
- cerr << "Problem opening data file!\n";
- exit(1);
- }
- for( int i=0; i < gNumRecs; i++)
- {
- ins << gRecs[i].StudentNo;
- ins << gRecs[i].FirstName;
- ins << gRecs[i].LastName;
- ins << gRecs[i].NumSubjects;
- for(int j=0; j < gRecs[i].NumSubjects; j++)
- {
- ins << gRecs[i].Subjects[j].Code;
- ins << gRecs[i].Subjects[j].Status;
- ins << gRecs[i].Subjects[j].Mark;
- }
- }
- ins.close();
- cout << gNumRecs << " records saved" << endl;
- }
- void UpdateRecord()
- {// updates status or mark of specified subject of specified student number
- int StudentNumber,subcode,markupdate;
- char smchange,statusupdate;
- ofstream ins;
- ins.open(cTextFileName);
- cout << "Enter student number: ";
- cin >> StudentNumber;
- int i = FindRecord(StudentNumber);
- if (i==-1)
- cerr << "Record not found!" << endl;
- else
- {
- PrintRecord(i);
- cout << " enter a subject code: ";
- cin >> subcode;
- if (!i== -1)
- {
- cout << "Select status or mark(s/m): ";
- cin >> smchange;
- switch(smchange)
- {
- case 's': cout << " select new status:" << endl;
- cout << "e: enrolled " << endl;
- cout << "p: provisional " << endl;
- cout << "w: withdrawn " << endl;
- cout << "status: ";
- cin >> statusupdate;
- break;
- case 'm': cout << "select new mark:" << endl;
- cin >> markupdate;
- markupdate=gRecs[i].Subjects[j].Mark;
- ins << markupdate;
- break;
- default: cerr << "invalid input (s/m)" << endl;
- }
- }
- else
- cerr << "Subject code not found!" << endl;
- }
- ins.close();
- }
- // ============= Private Function Definitions =========================
- void PrintRecord(int i)
- { // Prints record i on screen
- cout << "student number: \t" << gRecs[i].StudentNo << endl;
- cout << "first name: \t\t" << gRecs[i].FirstName << endl;
- cout << "last name: \t\t" << gRecs[i].LastName << endl;
- cout << "number of subjects: \t" << gRecs[i].NumSubjects << endl;
- for (int j=0; j < gRecs[i].NumSubjects; j++)
- {
- cout << gRecs[i].Subjects[j].Code << "\t";
- switch(gRecs[i].Subjects[j].Status)
- {
- case 0: cout << "enrolled\t";
- break;
- case 1: cout << "provisional\t";
- break;
- case 2: cout << "withdrawn\t";
- break;
- default: cerr << " status unresolved\t";
- }
- cout << gRecs[i].Subjects[j].Mark << "\n";
- }
- }
- int FindRecord(long StudentNo)
- {// returns index of matching record or -1 if not found
- for (int i=0; i < gNumRecs; i++)
- {
- if (gRecs[i].StudentNo == StudentNo)
- return i;
- }
- return -1;
- }