c++ - Issue with 2D Array -
i trying map out sudoku grid using 2d array in c++. have been testing code comparing input "dump" of 2d array. array 9x9. issue is first 8 columns perfect. lastly column seems wrong in 8 of 9 cases. why might be?
code:
#include <iostream> #include <fstream> #include <string> int = 0; int j = 0; const char test[12] = {'e', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'}; std::string abc = ""; // class map out sudoku grid class grid { public: char board[8][8]; void mapchars(std::string filename) { int x = 0; int y = 0; std::string line; std::ifstream myfile (filename.c_str()); if (myfile.is_open()) { while (getline(myfile,line)) { for(std::string::size_type = 0; < line.size(); ++i) { if(strchr(test, line[i]) != null){ if (line[i] == 'e') { y++; x=0; } else { board[y][x] = line[i]; x++; } } } } myfile.close(); } } void dumpmap() { while(j < 9){ while(i < 9){ std::cout << board[j][i] << ' '; ++i; } std::cout << std::endl; ++j; = 0; } } } sudoku; int main() { sudoku.mapchars("easy1.map"); sudoku.dumpmap(); std::cin >> abc; homecoming 0; }
you declare 8 8 array here: char board[8][8]. if want 9 9, utilize char board[9][9]. i'm guessing confused declaration indexing. when declare char board[8][8], board has first index runs 0..7 , similar sec index.
the reason value output instead of error because output of , index out-of-bounds access undefined. when code tries access board[i][j], executable utilize values gave i , j determine part of memory supposed retrieve info from. if i , j out-of-bounds, executable printing out memory not associated board @ all, , in fact garbage values, encountered.
c++ arrays multidimensional-array
No comments:
Post a Comment