1

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;
}
11
  • 1
    Unless this is an exercise in using pointers and dynamic allocation, don't do any of that. Use std::vector instead. Commented Sep 16, 2018 at 7:23
  • 1
    Other than that, why use a loop for the first input? And why not use a for loop for the other input? Commented Sep 16, 2018 at 7:25
  • As for your problem, can you please elaborate on it? Are the number you read form the file read correctly? Is new[] not throwing exceptions? Have you tried to debug your program? Does it do what you expect it to? Commented Sep 16, 2018 at 7:33
  • The first loop used to extract the value of first line from text file and break the loop The second loop used to extract value of second line till last line Commented Sep 16, 2018 at 7:42
  • Not throwing any exceptions. This is the output: numRoom:4 i:0 numstation:4 i:1 numstation:6 i:2 numstation:5 i:3 numstation:3 Commented Sep 16, 2018 at 7:44

1 Answer 1

1

A better way to do your current program, using std::vector, could be like this:

#include <iostream>
#include <vector>
#include <fstream>

int main()
{
    std::ifstream dataFile("data.txt");

    // Get the number of "rooms"
    unsigned roomCount;
    if (!(dataFile >> roomCount))
    {
        // TODO: Handle error
    }

    // Create the vector to contain the rooms
    std::vector<std::vector<int>> rooms(roomCount);

    for (unsigned currentRoom = 0; currentRoom < roomCount; ++currentRoom)
    {
        unsigned personCount;
        if (dataFile >> personCount)
        {
            rooms[currentRoom].resize(personCount);
        }
        else
        {
            // TODO: Handle error
        }
    }

    // Don't need the file anymore
    dataFile.close();

    // Print the data
    std::cout << "Number of rooms: " << rooms.size() << '\n';
    for (unsigned currentRoom = 0; currentRoom < rooms.size(); ++currentRoom)
    {
        std::cout << "Room #" << currentRoom + 1 << ": " << rooms[currentRoom].size() << " persons\n";
    }
}

As you can see, it's now possible to get the "sizes" of the data after you're done with the reading from the file.

Sign up to request clarification or add additional context in comments.

2 Comments

This method is easier but I think my lecturer wants me to use pointer method lol, just realised But somehow the size of room increased from 4 to 8 which supposed to be only 4
I am still thinking how to do with pointers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.