Pointers
This assignment demonstrates pointers in a variety of contexts and usages. It also uses the new and delete operators.
Create an application that reads country information from a given input file. The application counts the records in the file, then reads them and stores them in Country objects. The Country class is defined by the following code. It comprises the Country.h file that must be included in your project for full credit on this assignment. The name and capital members must be character arrays, and not string types.
class Country
{
private:
char* name_;
char* capital_;
long population_;
public:
Country (const char* country, const char* capital, long pop);
~Country ();
char* GetCountryName();
char* GetCapital();
long GetPopulation();
};
The application must have the following global variables, at file scope:
const int FILE_PATH_SZ = 512;
Country ** g_countryArray;
int g_arrSz;
The g_countryArray 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 Country objects that are dynamically instantiated by your program, one for each record of the input file.
This application should display a console menu that looks like this:
a)Read a text file
b)Remove countries starting with letter
c)Print all data to console
d)Quit
a)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 records in the file, and dynamically allocate an array for that many elements and store the pointer in g_countryArray. Then rewind to the beginning of the file (this can be done by closing the file and opening it again). Read one record at a time from the file again. For each record, the program must dynamically allocate a new Country object , passing in the Country data to the constructor. The address of the new Country object must be stored in next element in g_countryArray. The Country constructor must dynamically allocate space for character arrays to hold the name and the capital city, as well as a terminating null character, and assign the address of the character array to the data properties. The constructor must also set the population. When the whole file is read, the size of g_countryArray should equal the number of records 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_countryArray, then delete g_countryArray itself. Failure to do this will result in a memory leak. Do this in a function called FreeArray(). After g_countryArray has been deleted, be sure to set it to zero. If this is done, the FreeArray function can first check if g_countryArray is non-zero before deleting it so that you are not deleting memory that is not allocated.
b)The b) option should prompt the user to enter a single character. The program should then find and remove all countries in g_countryArray that start with that letter, in any case (upper or lower), making the selection in a case insensitive manner. Do not edit the input file, it should always contain the original four records. This option removes entries from the array, not the file. Note that you must use the “delete []” operator for each char array deleted otherwise you have a memory leak. Make sure to set the value of that array element to zero so you know it is empty.
c)The c) option should print the current contents of g_countryArray 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_countryArray if it is non-zero.
Make sure to add the following clear statement each time after closing any file (otherwise, some compilers, like mine, will not allow the file to be reopened and read correctly):
inFile.close();
inFile.clear(std::ios_base::goodbit);
Country class member functions:
The Country class must contain member functions that do work directly with individual Country objects.
- Country::Country (const char* country, const char* capital, long pop) - This constructor takes as arguments the data that is read from the input file, allocates two char arrays for the name and capital dynamically (using new), stores the address in the name_ and _capital properties and stores the population in the corresponding property.
- Country::~Country ()- The destructor must free the memory allocated in the constructor. Remember to use the square brackets (delete []) if the memory was allocated as an array. The destructor should also reset the properties to zero.
- The accessor functions return the appropriate data.
Your program should run as below, using the same menus and other verbiage as shown.
Usage Examples:
User entries are bolded:
Please make a selection:
a) Read a text file
b) Remove Countries starting with letter
c) Print Countries to console
d) Quit
a
Enter the full file path: C:\Users\USER_TEMPLATE\Downloads\NewHmwk1\countries.txt
Loaded file and read 4 Countries.
Please make a selection:
a) Read a text file
b) Remove Countries starting with letter
c) Print Countries to console
d) Quit
c
United Kingdom London 62000000
Denmark Copenhagen 5500000
Hungary Budapest 10000000
Germany Berlin 81600000
Please make a selection:
a) Read a text file
b) Remove Countries starting with letter
c) Print Countries to console
d) Quit
b
Enter the starting letter: g
Removed 1 Countries.
Please make a selection:
a) Read a text file
b) Remove Countries starting with letter
c) Print Countries to console
d) Quit
c
United Kingdom London 62000000
Denmark Copenhagen 5500000
Hungary Budapest 10000000
Please make a selection:
a) Read a text file
b) Remove Countries starting with letter
c) Print Countries to console
d) Quit
File Organization:
Put the Country class declaration in a header file called Country.h. Put the Country class definition in a source file called Country.cpp. Put the main program and all non-class functions into a main file called main.cpp.
Deliverables:
Upload the Country.cpp, Country.h, and main.cpp files only.