Reputation: 1999
I'm trying to read a text file with a 12x12 ASCII maze in it. However, all I'm getting on the screen is a 12x12 grid of asterisks.
I used similar code in a CLI minesweeper game I coded last semester and it worked fine. I'm not sure what I've done to cause it to not work...
Code:
bool loadBoard(Tile board [][gridSize], string filename) {
ifstream hndl;
char isWall;
hndl.open(filename);
// Check that the file is opened
if (hndl.is_open()) {
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
hndl >> isWall;
if (isWall == '*')
board[row][col].wall = true;
cout << row << col << isWall << " ";
}
cout << endl;
}
}
return EXIT_SUCCESS;
}
File maze.txt:
************
* * *
* * **** *
** * * *
*** *
* * * * * *
* * * * *
* * * * * *
* *
***** *** *
* * *
***********
Output:
00* 01* 02* 03* 04* 05* 06* 07* 08* 09* 010* 011*
10* 11* 12* 13* 14* 15* 16* 17* 18* 19* 110* 111*
20* 21* 22* 23* 24* 25* 26* 27* 28* 29* 210* 211*
30* 31* 32* 33* 34* 35* 36* 37* 38* 39* 310* 311*
40* 41* 42* 43* 44* 45* 46* 47* 48* 49* 410* 411*
50* 51* 52* 53* 54* 55* 56* 57* 58* 59* 510* 511*
60* 61* 62* 63* 64* 65* 66* 67* 68* 69* 610* 611*
70* 71* 72* 73* 74* 75* 76* 77* 78* 79* 710* 711*
80* 81* 82* 83* 84* 85* 86* 87* 88* 89* 810* 811*
90* 91* 92* 93* 94* 95* 96* 97* 98* 99* 910* 911*
100* 101* 102* 103* 104* 105* 106* 107* 108* 109* 1010* 1011*
110* 111* 112* 113* 114* 115* 116* 117* 118* 119* 1110* 1111*
Upvotes: 0
Views: 434
Reputation: 129464
Your code "skips" any whitespace. You can do cin >> noskipws >> isWall;
- or you could use a different character to show "not wall", such as '.'
or '-'
.
Upvotes: 6
Reputation: 3246
>>
operator ignores whitespace characters, that's why it skips over the blanks and always consumes an asterix. Use std::istream.get()
instead.
Upvotes: 2