Search code examples
c++arraysmultidimensional-arraycharifstream

Interger representing a Array value is increasing within a for loop when trying to assign characters to a 2D Character array


in my program I am attempting to input characters from a .txt file and assign them to a 2D Char array to form a Maze. The expected outcome of this code is:

xxxxxxx
xA...Bx
xxxxxxx

However I am instead warned that Column is greater than size (defined as 30) when I believe it should be 0 at the start of every loop. No matter what Column is equal to Size and im not sure why. I've included the code below. I am beginner programmer so if you have any advice could you please make it as simple as possible. Many thanks, Ben

#include<fstream>
#include<iostream>
#include<string>
#include <vector>
//Maze Size
#define SIZE 30
using namespace std;

void readMaze(string fileName);

void inputMaze();

int main()
{
    inputMaze();   
}

void readMaze(string fileName)
{
    int rows;
    int columns = 0;
    //vector<vector<char>> maze;    
    char maze[SIZE][SIZE];
    ifstream input(fileName);
    char data;
    while (input.get(data))   //While loop used to store each individual data to the string.
    {

        for (int rows = 0; rows < 20; rows++)
        {
            columns = 0;
            while (data != '\n')
            {
                if (rows > SIZE)
                {
                    cout << "ROWS GREATER THAN SIZE";
                    break;
                }
                else if (columns > SIZE)
                {
                    cout << "COLUMNS GREATER THAN SIZE";
                    break;
                }
                else
                {
                    maze[rows][columns] = data;
                    columns++;
                    data = input.get();
                }
            }
                data = input.get();
            }
        }
        cout << "The Maze being solved is: " << endl;
        cout << maze << endl;
        input.close();
}


void inputMaze()
{
    string userinput;
    cout << "Plese input a .txt file name" << endl;
    cin >> userinput; //User inputs the name of a .txt file --> goes to readMaze()
    readMaze(userinput);
}

Solution

  • rows is used uninitialized in readMaze so the program has undefined behavior. Also, cout << maze << endl; makes the program have undefined behavior by reading out of bounds.

    Consider making maze a std::vector<std::vector<char>> or even a std::vector<std::string> to make it simpler.

    Example:

    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #include <iterator>
    #include <sstream>
    #include <string>
    #include <vector>
    
    std::vector<std::string> readMaze(std::istream& input) {
        std::vector<std::string> maze;
        std::string line;
    
        while(std::getline(input, line)) { // read a complete line at a time
            maze.push_back(line);          // and save it in the vector 
        }
    
        return maze;
    }
    
    void inputMaze() {
        std::string userinput;
        std::cout << "Plese input a .txt file name\n";
    
        if(std::cin >> userinput) {
            std::ifstream is(userinput);
            if(is) {
                auto maze = readMaze(is);
    
                std::cout << "The Maze being solved is:\n";
                std::copy(maze.begin(), maze.end(),
                          std::ostream_iterator<std::string>(std::cout, "\n"));
            }
            // the file will be closed automatically when "is" goes out of scope
        }
    }
    
    int main() {
        inputMaze();   
    }