Basically, I want to be able to take the content of a file and put them into a 2D array. Each line would correspond with each row. Problem is, I have no idea how to do this. I don't even know if this is possible.
Right now, all I know is that I have to use get.line
After passing it into an array, I want to be able to check the value of all the arrays, and if they equal to a value, replace them. (Like take all the W from the array and change them to S)
#include <iostream>
#include <fstream>
usingnamespace std;
void main (void)
//char map_three (char map[20][20])
{
ifstream inData;
inData.open ("file1.txt"); //open file
while (!inData.eof()) { //read from input files
getline( inData, data);
//not sure what to do here.
}
int rows = 20; //create array using pointers and double pointer
int ** maze = newint * [rows];
int cols =60;
for (int r = 0; r < rows; r ++)
{
maze[r] = newint [cols];
}
}
It would help if you show sample file contents, the file format is not obvious from the description.
For example, if you're reading every character from the line, your data structure could be a simple vector of strings:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> maze;
std::ifstream inData("file1.txt"); //open file
std::string line;
while ( getline(inData, line) ) //read until eof
maze.push_back(line);
// replace every 'W' with 'S'
for(auto& line: maze)
std::replace(line.begin(), line.end(), 'W', 'S');
// output
for(auto& line: maze)
std::cout << line << '\n';
}
but if you have, for example, space-separacted characters (e.g ". . . S ." rather than "....S..", then conversion of line into the string to be pushed back would be a little different.
This is what I want to pass in, with all the underlines to be changed to spacebars and the Ws changed to another ASCII character. This is a map for a simple game.
And in your code, for(auto& line: maze)
I don't understand this. I thought for is a loop? And what is auto and &?