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;
}

Just off the top of my head:

Constructors should result in a fully functional instance of the class. If you pass a filename to the constructor, you should use it in the constructor to populate the data. On the other hand, an empty array is also fully functional, so it might be as good or better to pass the filename to the populate() method.

In any case, as a general rule, it is better to release resources as soon as feasible, so keeping the file open during the lifetime of the object is probably not the right thing to do.

so it's probably better not to put an empty array yet?

commented: Your links don't show up on Google anyway -1

so it's probably better not to put an empty array yet?

I never said that. In fact the opposite. The KISS principle suggests that a constructor should do as little as possible (so as to not do something that may turn out to be the wrong thing when you derive from the class, or use it a way that wasn't first intended). So what I said was that the constructor probably should not have a filename as a parameter because an empty array is sane, simple. Or, if the array really is tightly bound to its data file, then the constructor needs to finish the job and actually populate the array before it returns. If that is so, the the populate() method might better be private, and be called from the constructor.

In either case, once the file's data has been poured into the array, the file should be closed (*). Open files are a limited resource that should be returned to the system asap. Once again it is KISS: Use it, close it, forget it. I would pass the file name to the populate() method, open it there, use it, and close it there. If the array needs to keep track of its file after it is populated, it should keep it as a path (name) rather than an open file.

(*) I can imagine use cases for keeping the file open, but this does not appear to need it.

commented: thanks a bunch +1

thanks great advice! I was also told the constructor should do little as possible, how should i fix this in my code? is it ok for it to try and allocate dynamic memory in my CSTOR or is this bad?

KISS (aka "the principle of parsimony") says if you don't need it now, don't do it now: You may never need it. Imagine someone uses this class as the data in a sparse array of 100,000 items, only a few of which are non-empty. Should the class allocate memory before it is needed?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.