...">

Reading in ascii maze into a 2d array

I am writing code to read in a 7x15 block of text in a file that will be a "maze".

#include <iostream>
#include <fstream>
#include <string>
#include "board.h"  

int main()
{
    char charBoard[7][15];  //the array we will use to scan the maze and modify it
    ifstream loadMaze("maze");  //the fstream we will use to take in a maze
    char temp; //our temperary holder of each char we read in

    for(int i = 0;i < 7; i++)
    {

        for(int j = 0; j < 15; j++)
    {
        temp= loadMaze.get();
        charBoard[i][j] = temp;
        cout << charBoard[i][j];  //testing
    }
    cout << endl;
}

return 0;
}

      

It was my original project, but it didn't work as it kept coming back? for each char, it is read. This is testing the maze with:

  #############
              #
############ #
              #
 ######### ####
 #! #   
############   

EDIT: Cout prints this:

  #############


#
############ 
 #

  #
 ######### 
####
 #!      
 #   
#########

Am I not running \ n's?

I have been coding for hours, so I think this is a simple error that I won't catch, which is turning me off right now. Thank!

+2


source to share


4 answers


Try an absolute path like "c: \ MyMazes \ maze".

Throw in ("cd") to see where the current directory is. If you have trouble finding the current directory check out this SO discussion



Here's the complete code - this should display your entire maze (if possible) and your current directory.

 char charBoard[7][15];      //the array we will use to scan the maze and modify it
 system("cd");
     ifstream loadMaze("c:\\MyMazes\\maze");  //the fstream we will use to take in a maze

 if(!loadMaze.fail())
 {
    for(int i = 0;i < 7; i++)
    {
        // Display a new line
        cout<<endl;
        for(int j = 0; j < 15; j++)
        {
             //Read the maze character
             loadMaze.get(charBoard[i][j]);
             cout << charBoard[i][j];  //testing
        }
        // Read the newline
        loadMaze.get();
    }
    return 0;
 }
 return 1;

      

+3


source


Is it possible to check the correctness of the extraction from the file: using the good()

APIifstream

for(int j = 0; j < 15; j++)
{
    if(!loadMaze.good())
    {
        cout << "path incorrect";

    }

    temp= loadMaze.get();


    cout << "temp = " << temp << endl; //testing
    charBoard[i][j] = temp;
    cout << charBoard[i][j];  //testing
}

      

OR



at the beginning:

ifstream loadMaze("maze"); 
if(!loadMaze.good())
{
  //ERROR
}

      

0


source


try adding the line

if (!loadMaze) throw 1;

      

after declaring loadMaze, this will throw an exception if the file is not there. This is a hack, in fact you should be throwing the real error. But it works for testing purposes.

0


source


Check if the file has opened or not. You can find out by checking if it's good:

http://www.cplusplus.com/reference/iostream/ios/good/

If opening the file fails, try writing the absolute path to the file (C: / Documents and Settings /.../ maze) to see if that works. If so, it's just the wrong file path and you'll have to play with it.

0


source







All Articles