Pointer and global variable frustration

I'm to create an application that reads words from a given input file. The application counts the words in the file, then reads them and stores them in Word objects. The Word class is defined by the following code.

class Word

{

private:

char* ptr_;

int len_;

public:

Word(char* word);

~Word();

char GetFirstLetter();

char* GetWord();

};

The application must have the following global variables, at file scope:

const int FILE_PATH_SZ = 512;

Word** g_wordArray;

int g_arrSz;

The g_wordArray variable is a pointer to a pointer. In this program it will be used as an array of pointers. In its elements it will contain pointers to Word objects that are "dynamically instantiated" by the program, one for each word in a file that it reads.

This application should display a console menu that looks like this:

Read a text file

Remove words starting with letter

Print words to console

Quit

When a) is selected, the program must prompt the user to enter a file path for an input text file. It should open the file, count the number of words in the file, and dynamically allocate an array for that many elements and store the pointer in g_wordArray. Then rewind to the beginning of the file (this can be done by closing the file and opening it again). Read one word at a time from the file again. For each word, the program must dynamically allocate a new Word object , passing in the word to the constructor. The address of the new Word object must be stored in next element in g_wordArray. The Word constructor must dynamically allocate space for a character array to hold the word, as well as a terminating null character, and assign the address of the character array to the ptr_ property. The constructor must also set the len_ property and copy the word into the new character array. When the whole file is read, the size of g_wordArray should equal the number of words in the file.

Note that this menu selection may be chosen even after a file has been open. In that case, the program must delete all pointers stored in g_wordArray, then delete g_wordArray itself. Failure to do this will result in a memory leak. Do this in a function called FreeWordArray(). After g_wordArray has been deleted, be sure to set it to zero. If this is done, the FreeWordArray function can first check if g_wordArray is non-zero before deleting it.

The b) option should prompt the user to enter a single character. The program should then find and remove all words in g_wordArray that start with that letter, in any case (note that you must use the “delete []” operator otherwise you have a memory leak). Make sure to set the value of that array element to zero so you know it is empty. Also, write the contents of the g_wordArray to the input text file so it is up to date with g_wordArray.

The c) option should print the current contents of g_wordArray to the console, separated by spaces, which should match the contents of the text file. Be sure to only print the array element of g_wordArray if it is non-zero.
And then q) obviously quits
These are just switch statements, it's the statements themselves using pointer/global variables I'm having trouble with...
Post some code with a specific question.
Topic archived. No new replies allowed.