I am trying to dynamically allocate memory for a nested struct that happens to be a pointer. I have written some made-up code below to try and illustrate my problem.
These two structs are found in two separate header files, also the code is under one namespace.
Tiles.h
struct Tiles
{
int* m_noOfSections;
int* m_noOfTilesInSecs;
char* m_TileName;
};
// functions omitted
Flooring.h
struct Flooring
{
Tiles* m_Tiles;
int m_noOfTiles;
char* m_FlooringName;
};
void read(Tiles&);
I am working on a function definition for Flooring.h where I have to dynamically allocate an array of Tiles, the size of the array is determined earlier on in this function from user input.
I've tried using the following code but ran into issues:
Flooring.cpp
void read(Flooring&Flr)
{
Tiles* tiles;
tiles = new Tiles[Flr.m_noOfTiles];
for (int i = 0; i < Flr.m_noOfTiles; i++) {
cout << i + 1 << ": ";
read(tiles[i]);
}
}
Note: The read(tiles[i]); [declaration: void read(Tiles&)] function assigns values to the data members of Tiles. I have tested the functions in the Tiles files and they are working as intended. So I have not included the code for those. I believe the issue lies in my Flooring.cpp implementation file.
My expectation is that that the above read function would assign values to:
tiles[i].m_noOfSections, tiles[i].m_noOfTilesinSecs, tiles[i].m_tileName
One of the issues is that I do not have a chance to input tileName, when running the code it skips the part where I would normally input a tileName.
The output would be as follows:
Enter the number of sets of tiles: Enter number of sections: [user is able
to input value here, but not before when asked to enter the number of the set of tiles]
Tiles.cpp
void read(char* tileName)
{
cout << "Enter tile name: ";
read(tileName, 15, "error message") ;
}
The function definition for the read function with three parameters can be found below. This function was pre-defined in this assignment. I have also reviewed the function and I don't see any problems with it, but I will post it regardless if it helps.
void read(char* str, int len, const char* errorMessage)
{
bool ok;
do
{
ok = true;
cin.getline(str, len+1, '\n');
if (cin.fail()) {
cin.clear();
cin.ignore(1000, '\n');
ok = false;
}
}while(!ok && cout << errorMessage);
}
I hope that is enough information, apologies if my formatting isn't adequate, or if my terminology isn't appropriate, I am still quite new to all sorts of programming. Please do let me know if I have forgotten to include some information.
mainfunction and input which the user enters. See also minimal reproducible example.