Scenario: Read numbers from file and create dynamic 2d array accordingly The first line of data file represents the rooms and rest of the lines represent the number of person in the room
For example:
4 4 6 5 3
total 4 rooms, 1st room has 4 people, 2nd room has 6 people...
So far this is my code, how do I check I've created the dynamic array with correct size?
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
ifstream readFirstLine("data.txt");
ifstream readData("data.txt");
string line;
int numRoom, numPerson = 0;
int i = -1;
while (getline(readFirstLine, line))
{
istringstream linestream(line);
if (i == -1)
{
linestream >> numRoom;
cout << "numRoom:" << numRoom << endl;
break;
}
}
readFirstLine.close();
int** numRoomPtr = new int*[numRoom];
while (getline(readData, line))
{
istringstream linestream(line);
if (i == -1)
{
}
else
{
linestream >> numPerson;
numRoomPtr[i] = new int[numPerson];
cout << "i:" << i << endl;
cout << "numPerson:" << numPerson<< endl;
}
i++;
}
readData.close();
return 0;
}