gpt4 book ai didi

c++ - 基于用户输入的二维动态数组

转载 作者:行者123 更新时间:2023-11-30 04:56:47 27 4
gpt4 key购买 nike

<分区>

场景:从文件中读取数字并相应地创建动态二维数组数据文件的第一行代表房间,其余行代表房间内的人数

例如:

44653

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;
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com