Computer Science 2574
Intro to Data Structures & Soft Eng

Build.cpp

#include "buildodb.h"


//*********************************************************************************
// Function:	 BuildODB
// Purpose:		 To build the ODB file for viewing and storage.
// Called By:	 FileMenu
// Calls:		 getResFileName, getDecodeFileName, getODBName, CopyCrseInfo,
//				 getNumKeys, getKeys, redirectoRES, ResetMarker, BuildList
// Parameters:	 AStudent[GroupSize]		struct that holds student test info
//			     inFile						input file stream var.
//				 outFile					output file stream var.
//				 ResFileName[NameLength]	var. that holds result file name
//				 ATest[GroupSize]			struct that holds test answers
//		         inFileDecode				input file stream var.
//				 DecodeFileName[NameLength]	var that holds decode file name
//
// Return value: void
// Authors:		 John W. Boyd III & Cullen J. Morris
// Version:		 1.1
//**********************************************************************************

void BuildODB()
{
	ofstream datafile;
	ofstream outFile;
	ifstream inFile;
	ifstream inFileDecode;
	ifstream inFileODB;
	
	int CorrectNum,
		OmittedNum,
		NumSeat,
		GroupNum,
		NumStudents,
		NumKeys = 0,
		savenum = 0,
		i = 0,
		j = 0, 
		k = 0;
	
	double CorrectPercent,
		   TscoreStat;

	char DSocSecNum[SSLength],
		 RSocSecNum[SSLength],
		 Lastname[NameLength],
		 FormCh,
		 FormLetter,
		 ch,
		 GroupCh,
		 AnswerCh,
		 answers[NumAnswers],
		 ResFileName[NameLength],
		 DecodeFileName[NameLength],
		 ODBFileName[NameLength],
		 ODBExt[ExtensionLength] = ".odb";

	float NumQuestions;

	getResFileName(ResFileName, inFile);
	cout << endl << endl;
	getDecodeFileName(DecodeFileName, inFileDecode);
	cout << endl << endl;
	getODBname(ODBFileName, ODBExt, outFile);

	datafile.open(tempfile);
	datafile << ODBFileName;
	datafile.close();

	inFileDecode.ignore(350, '\n');
	inFileDecode >> NumQuestions;
	inFileDecode.ignore(350, '\n');
	
	CopyCrseInfo(NumStudents, inFile, outFile);
	getNumKeys(NumKeys, inFileDecode);
	outFile << NumStudents << "  " << NumQuestions;
	outFile << "  " << NumKeys << endl;
	
	getKeys(outFile, inFileDecode, NumQuestions);
	
	ResFileInfo AStudent[NumAnswers];
	DecodeFileInfo ATest[NumAnswers];
	
	while (i < NumStudents) 
	// puts info into structs
	{
		NumStudents--;
		inFile.get(RSocSecNum, 10);
		strcpy(AStudent[i].SSNum, RSocSecNum);
		inFile.ignore(2, '\n'); //get the last name, if there is one
		inFile.get(ch);
		
		if (islower(ch) || isupper(ch))
		// makes sure the  ssn is valid
		{
			inFile.putback(ch);
			inFile.get(Lastname,22);
			strcpy(AStudent[i].LastName, Lastname);
		}
		else
			strcpy(AStudent[i].LastName, NoName);
			
		inFile >> CorrectNum;  //get the number of correct answers
		AStudent[i].NumCorrect = CorrectNum;

		inFile >> OmittedNum;  //get the number of omitted answers
		AStudent[i].NumOmitted = OmittedNum;

		inFile >> NumSeat;  //get the seat number
		AStudent[i].SeatNum = NumSeat;

		inFile >> FormLetter;  //get the form letter
		AStudent[i].Form = FormLetter;

		inFile >> GroupNum; //get the group number
		AStudent[i].Group = GroupNum;

		inFile >> CorrectPercent;  //get the percentage of correct answers
		AStudent[i].PercentCorrect = CorrectPercent;

		inFile >> TscoreStat;  //get the student's Tscore
		AStudent[i].Tscore = TscoreStat;

		inFile.ignore(100, '\n');

        redirectoRES(outFile, AStudent[i]); //write the records to the file
		
		
		// Finished processing the student information from the results file.  Now
		// to get the corresponding student answers from the decode file.	
			
		inFileDecode.get(DSocSecNum, 10);
		
		while (strcmp(DSocSecNum, RSocSecNum) != 0)
		// matches up student in each file
		{
			inFileDecode.ignore(100, '\n');
			inFileDecode.get(DSocSecNum, 10);
		}

		strcpy(ATest[i].SSNum, DSocSecNum);
		inFileDecode.get(FormCh);
		inFileDecode.get(GroupCh);
			
		for ( k=0; k < (NumQuestions + 10); k++)
		//we added 10 to get the 10 whitespaces after the last answer
		{
			inFileDecode.get(AnswerCh);
			switch (AnswerCh)
			{
				case ' ': AnswerCh = '?';
								 break;
				case '*': AnswerCh = '*';
								 break;
			}

			answers[k] = AnswerCh;
				
		} //end for
		
		answers[int(NumQuestions + 10)] = '\0';

		inFileDecode.ignore(350,'\n');
		strcpy(ATest[i].Answers, answers);
				
		outFile << ATest[i].Answers << endl; //write the records to the file
		
		ResetMarker(inFileDecode);
		inFileDecode.ignore(350, '\n');
		inFileDecode.ignore(350, '\n');

	} //end while

	inFile.close();
	inFileDecode.close();
	outFile.close();
	inFileODB.open(ODBFileName);// opens ODB file for reading
	
	int zero = 0;
	
	BuildList(inFileODB, zero);
}



//*********************************************************************************
// Function:	 OpenPreviousODB
// Purpose:		 To open the ODB file for viewing
// Called By:	 Open, OpenPreviousODB
// Calls:		 fexist, BuildList		 
// Parameters:	 NONE
// Return value: void
// Authors:		 John W. Boyd III & Cullen J. Morris
// Version:		 1.1     
//**********************************************************************************

void OpenPreviousODB()
{

	char readODBname[NameLength];

	int savenum = 0;

	ifstream inFileODB;
	ofstream outFile;
	ofstream datafile;
	
	cout << endl << "Please enter the full name (plus extension) of the ODB";
	cout << " file." << endl;
	cout << "Then press enter to continue.";
	cout << endl;
	cout << endl;

	cin >> readODBname;

	datafile.open(tempfile);
	datafile << readODBname;
	datafile.close();
	
	if (fexists(readODBname))
	// opens inFileODB if it exists
	{
		int zero = 0;

		cout << endl << endl;
		inFileODB.open(readODBname);
		BuildList(inFileODB, zero); 
	}
	else
	//opens inFileODB when it exists
	{
		  cout<< readODBname << ": does NOT exist" << endl << endl;
		  OpenPreviousODB();
	}
	
	cout << endl;
	cout << endl;

} //end of OpenPreviousODB



//************************************************************************************
// Function:	 redirectoRES
// Purpose:		 BuildODB
// Called By:	 EditMenu
// Calls:		 NONE
// Parameters:	 outFile - output file for writing
//				 AStudent - Struct that holds student info
// Return Value: void
// Authors:		 John W. Boyd III & Cullen J. Morris
// Version:		 1.1     
//************************************************************************************

void redirectoRES(ofstream &outFile, ResFileInfo AStudent)
{
	outFile.setf(ios::fixed, ios::floatfield);
	outFile.setf(ios::showpoint);

	outFile << setw(9) << AStudent.SSNum << " ";
	outFile << setw(18) << setiosflags(ios::left) << AStudent.LastName << " ";
	outFile << setw(3) << AStudent.NumCorrect << " ";
	outFile << setw(2) << AStudent.NumOmitted << " ";
	outFile << setw(2) << AStudent.SeatNum << " " << AStudent.Form;
	outFile << " " << AStudent.Group << " " << setw(5);
	outFile << setprecision(1) << AStudent.PercentCorrect << "  ";
	outFile << setw(5) << setprecision(1) << AStudent.Tscore << endl;

} //end of redirectoRES


//*********************************************************************************
// Function:	  ResetMarker
// Purpose:		  This function repositions the file marker to the start of the file
// Called By:	  BuildList, BuildODB, GetNumKeys 
// Calls:		  NONE
// Parameters:    infile - input file
// Return value:  integer 0.
//*********************************************************************************

int ResetMarker (ifstream &infile)
{
    infile.seekg(0); // after opening and reading
                     //reposition file marker at the start
					 // more reading
    return 0 ;
} //end of ResetMarker