I'm trying to read data from a grid, in a 20x20 file. I'm using a two-dimensional vector of vectors of strings.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
#define HEIGHT 20
#define WIDTH 20
typedef vector<vector<string> > stringGrid;
bool readGrid(stringGrid& grid, string filename) {
grid.resize(HEIGHT);
for (int i = 0; i < HEIGHT; i++)
grid[i].resize(WIDTH);
ifstream file;
string line;
file.open(filename.c_str());
if (!file.is_open()) {
return false;
}
for (int i = 0; i < HEIGHT; i++)
{
while (getline(file, line)) {
grid[i].push_back(line);
}
}
return true;
}
void displayGrid(stringGrid grid)
{
for (int row = 0; row < HEIGHT; row++)
{
for (int col = 0; col < WIDTH; col++)
{
cout << grid[col][row];
}
cout << endl;
}
}
int main(){
stringGrid grid;
readGrid(grid, "test.txt");
displayGrid(grid);
return 0;
}
However, when I run this code, the program only outputs a few blank lines. Why doesn't this code work? The logic seems sound enough.