any suggestions on this custom array class?(it compiles and works properly)
// Sequential_Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <WinBase.h>
/**
* Class: Array
*
* Custom Array class used to read input from a file
* and store the contents to specific members of Node.
* 1 Node is equivalent to 1 person.
*
*/
class Array {
private:
class Node {
private:
friend class Array;
int m_SSid;
int m_age;
int m_commulativeContribution;
int m_yearStarted;
public:
};
Node *m_headPtr;
int m_ArraySize;
std::fstream m_dataFile;
public:
Array( int size, std::string fileName );
~Array();
void populateArray();
void displayArray();
void displayElement( int index );
int sequentialSearch( int SSid );
};
/**
* Constructor: Array()
*
* The constructor must accept the size of the list to be used
* as well as the name of the file which contains the data of
* each Node/Person. If the file fails to Open for input then
* throw an exception which is caught and the error is stored in
* a log file. If successfull then allocate space for our custom
* array depending on the size specified by the user/programmer.
* If unable to allocate memory on the heap for the custom array close
* the file and then throw an exception which is caught by the caller
* and stored into a log file.
*
* @access public
*/
Array::Array( int size, std::string fileName )
: m_ArraySize( size )
{
m_dataFile.open( fileName.c_str(), std::ios::in );
if( m_dataFile.fail() )
throw "Unable To Open The Specified File";
m_headPtr = new Node[size]; // allocate memory for Array
if( m_headPtr == NULL ) {
m_dataFile.close();
throw "Unable To Allocate Sufficient Memory On The Heap";
}
}
Array::~Array()
{
m_dataFile.close();
delete [] m_headPtr;
}
/**
* Function: populateArray()
*
* Simply progresses through the custom array and populates it
* with data from the specified input file. Must clear eof/status
* flags for the ofstream and then reset file cursor to the beginning
* of the file in order to make this function call to be successfull
* multiple times
*
* @return void
* @access private
*/
void Array::populateArray()
{
m_dataFile.clear();
m_dataFile.seekp( 0L, std::ios::beg ); // (offset, base(where to start))
for( int i = 0; i < m_ArraySize; i++ ) {
m_dataFile >> m_headPtr[i].m_SSid;
m_dataFile >> m_headPtr[i].m_age;
m_dataFile >> m_headPtr[i].m_commulativeContribution;
m_dataFile >> m_headPtr[i].m_yearStarted;
}
}
/**
* Function: displayArray()
*
* Simply Progresses through the whole list
* and displays the datamembers of each Node/Person
* element in the list.
*
* @return void
* @access private
*/
void Array::displayArray()
{
for( int i = 0; i < m_ArraySize; i++ ) {
std::cout << m_headPtr[i].m_SSid << std::endl;
std::cout << m_headPtr[i].m_age << std::endl;
std::cout << m_headPtr[i].m_commulativeContribution << std::endl;
std::cout << m_headPtr[i].m_yearStarted << std::endl << std::endl;
}
}
/**
* Function: displayElement()
*
* Recieves an Index to where the located Person/Node
* object is located in the list, and then dereferences
* the object's data members there and displays them.
*
* @return void
* @access private
*/
void Array::displayElement( int i )
{
std::cout << m_headPtr[i].m_SSid << std::endl;
std::cout << m_headPtr[i].m_age << std::endl;
std::cout << m_headPtr[i].m_commulativeContribution << std::endl;
std::cout << m_headPtr[i].m_yearStarted << std::endl;
}
/**
* Function: sequentialSearch()
*
* Simply Progresses through the whole list
* and when a match for the specified passed in SSid
* is found, then return the index to which that
* SSid number was found. Returns -1 if no match was found
*
* @return bool
* @access private
*/
int Array::sequentialSearch( int SSid )
{
for( int i = 0; i < m_ArraySize; i++ )
if( m_headPtr[i].m_SSid == SSid )
return i;
return -1;
}
/**
* Function: add_log()
*
* Whenever an exception is thrown and caught
* this function is called to write to a text
* file what exception was thrown, and when it
* happened.
*
* @return void
* Free Function
*/
void add_log(char buffer[100])
{
std::ofstream outFile;
outFile.open("Log.txt", std::ios::app);
SYSTEMTIME st;
GetLocalTime(&st);
outFile << "===================================" << std::endl;
outFile << "Date: " << st.wMonth << "/" << st.wDay << "/" << st.wYear << std::endl;
outFile << "Time: " << st.wHour << ":" << st.wMinute << std::endl;
outFile << "===================================" << std::endl;
outFile << "[ERROR] - " << buffer << std::endl;
outFile.close();
}
/**
* Function: holdScreen()
*
* This function is pretty self explanatory.
* All it does is hold the console screen open.
* It does this by ignoring any input left in the
* input stream, and then asking the user to type
* a character which is pretty much used to exit
* the program in this case
*
* @return void
*/
inline void holdScreen()
{
std::cin.ignore();
std::getchar();
}
int _tmain(int argc, _TCHAR* argv[])
{
try {
Array Array1( 2, "datafile.txt" );
Array1.populateArray();
Array1.displayArray();
int index = Array1.sequentialSearch( 645 );
if( index == -1 )
std::cout << "The SSid that was searched for was not found in the Array" << std::endl;
else
std::cout << "The SSid was found at index " << index << std::endl;
}
catch( char *string ) {
add_log( string );
}
holdScreen();
return 0;
}