<分区>
场景: 从文件中读取数字并相应地创建动态二维数组 数据文件的第一行代表房间,其余行代表房间内的人数
例如:
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;
}