T.J. Williams
T.J. Williams

Reputation: 11

using getline to input into 2D array

for one of my assignments i have to cin a 2D array using getline. the maze design is made up on the spot.

16 10
################
#      #    #  #
# # #### ##   ##
# #      #######
# ###### #E    #
#S# #  # ### ### 
# # ## #     # #
# # ## ####### #
#              #
################

this is one of the sample inputs thats going to test our backtracking algorithm.

the 16 10 is the Column and Row of our maze.

i was wondering how i would go about to parse getline correctly so that my 2D array would fill using the maze given.

on a side note, i made a practice one where i dont have to cin and instead already have my array and i was wondering how i could tell it to start at the S.

sorry if theres a question on this but i didnt really see one where it was getlining into a 2D array in this format where you dont know your array size.

Upvotes: 0

Views: 5495

Answers (2)

Geoff Montee
Geoff Montee

Reputation: 2597

Try this:

size_t num_rows;
size_t num_cols;

cin >> num_rows >> num_cols;

char* maze = new char[num_rows * num_cols];

for (size_t row = 0; row < num_rows; row++)
{
     string line;

     getline(cin, line);

    if (line.size() != num_cols)
    {
        cerr << "Error! Size is " << line.size() << " rather than " << num_cols << endl;
        exit(1);
    }

    for (size_t col = 0; col < num_cols; col++)
    {
        maze[(row * num_cols) + col] = line[col];
    }
}

cout << "Maze is: " << endl;

for(int row = 0; row < num_rows; row++)
{
    for(int col = 0; col < num_cols; col++)
    {
        cout << maze[(row * num_cols) + col];
    }

    cout << endl;
}

delete [] maze;

To figure out where start is:

size_t start_row, start_col;

for(int row = 0; row < num_rows; row++)
{
    bool found = false;

    for(int col = 0; col < num_cols; col++)
    {
        if (maze[(row * num_cols) + col] == 'S')
        {
            start_row = row;
            start_col = col;
            found = true;
            break;
        }
    }

    if (found)
    {
        break;
    }
}

You can do similar things for the end point.

If you want to put the start point at a random empty spot, you can use srand and rand.

First, seed the pseudorandom number generator at the beginning of your program:

srand(time(0));

Then, determine a random start point:

size_t start_row, start_col;
bool found = false;

while (!found)
{
    start_row = rand() % num_rows;
    start_col = rand() % num_cols;

    if (isspace(maze[(start_row * num_cols) + start_col]))
    {
        maze[(start_row * num_cols) + start_col] = 'S';
        found = true;
    }
}

You can put the end spot in a random empty spot in a similar way.

People will sy that srand and rand aren't very good at generating random numbers. That is true, but it should be sufficient for your needs.

Upvotes: 0

hexist
hexist

Reputation: 5240

getline will only read one line at a time, so what you'll probably want to do is use a for loop to read each line in turn and store it as one row of your 2d array.

Upvotes: 1

Related Questions