Anyone know how to store pointers in a multi-dimensional array? I think that might be the problem that i am having in main:
// main.cpp
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <fstream>
#include <string>
#endif
#include "Word.h"
using namespace std;
const int WORD_SZ = 100;
Word** g_wordArray;
int g_arrSz;
static char filePath[ FILE_PATH_SZ ] = {};
void FreeWordArray();
int main( const int argc, const char **argv )
{
int
wrdCount = 0;
char
usrMenuOption = 0,
getFirstLetter = 0,
tmpArray[WORD_SZ] = {},
*getWord = new char;
string
str,
str2;
ifstream
inFile,
inFile2;
do
{
cout << "Please make a selection: \n\
a) Read a text file\n\
b) Remove words starting with letter\n\
c) Print words to console\n\
d) Quit\n";
cin >> usrMenuOption;
switch( usrMenuOption )
{
case'A':
case'a':
cout << "Enter a file name: ";
cin.sync();
cin >> filePath;
inFile.open( filePath );
if ( !inFile ) return -1;
inFile >> tmpArray; // prime the eof flag.
while ( !inFile.eof() )
{
inFile >> tmpArray;
wrdCount++;
g_wordArray = new Word *[wrdCount];
}
inFile.close();
inFile2.open( filePath );
while( !inFile2.eof() )
{
inFile2 >> tmpArray;
// supplies the member functions with information from the file
g_wordArray[wrdCount] = new Word( tmpArray );
g_wordArray[wrdCount]->GetFirstLetterLower();
g_wordArray[wrdCount]->GetWord();
}
cout << wrdCount << " Words read from the file " << endl;
inFile2.close();
break;
case'B':
case'b':
// information not found returning null
g_wordArray[wrdCount]->GetFirstLetterLower();
break;
case'C':
case'c':
g_wordArray[wrdCount]->GetWord();
break;
case'D':
case'd':
cout << "Quit Requested. " << endl;
break;
default:
cout << '"' << usrMenuOption << '"' << " Not Defined! " << endl;
}
} while ( usrMenuOption != 'D' && usrMenuOption != 'd' );
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
cin.ignore();
return 0;
}
void FreeWordArray()
{
delete[ ] g_wordArray;
return;
}
// Word.cpp
#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"
#include <vector>
using namespace std;
// No paramaters.
// Is this what im missing?
// I just threw it in because of an error.
Word::Word()
{
}
Word::Word( const char* word )
{
ptr_ = new char[ strlen( word ) + 1 ];
strcpy( ptr_, word );
len_ = strlen( ptr_ );
}
Word::~Word()
{
delete[ ] ptr_;
ptr_ = NULL;
len_ = NULL;
}
char Word::GetFirstLetterLower()
{
char myChar = tolower( ptr_[0] );
return myChar;
}
char* Word::GetWord()
{
Word *objectOne = new Word;
objectOne->ptr_ = ptr_;
strcpy( objectOne->ptr_, ptr_ );
return objectOne->ptr_;
}
My Goal is to have all the words read from the file in my g_wordArray[wrdCount]->SomeFunction()
without being dependent in the file reading loop.
What i've been trying to do:
In the Implementation File, under the
getFirstLetterLower
: to add thechar *_ptr
private member to a new variable each time. likesomeCharVar[0] = firstWord
,someCharVar[1] = secondWord
...Reading the contents of the file into a single varaible. Looping through that varaible in each case that i need.
I like that idea, but haven't figured out how to do it. Any suggestions?