I have created a code which outputs my array into a 2 dimensional grid, something like x and y axis. Currently the code and the output:
Code:
char array[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
array[i][j] = '0';
}
}
for (int i = 0; i < 9; i++)
{
cout << i << " ";
for (int j = 0; j < 9; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
cout << " ";
for (int i = 0; i < 9; i++)
cout << i << " ";
cout << endl;
Output:
0 O O O O O O O O O
1 O O O O O O O O O
2 O O O O O O O O O
3 O O O O O O O O O
4 O O O O O O O O O
5 O O O O O O O O O
6 O O O O O O O O O
7 O O O O O O O O O
8 O O O O O O O O O
0 1 2 3 4 5 6 7 8
Now i have a file, inside filled with coordinates that i'm suppose to mark out. The problem is how do i mark out all the coordinates, say to put a '1' on all the coordinates marked, on the grid that i've done. Firstly, i have declared my ifstream and managed to read its contents. And now i'm stuck! Any help would be appreciated.
This is the file content:
[1, 1]
[1, 2]
[1, 3]
[2, 1]
[2, 2]
[2, 3]
[2, 7]
[2, 8]
[3, 1]
[3, 2]
[3, 3]
[3, 7]
[3, 8]
[7, 7]
ifstream
has a constructor that gets your file's path. To read the chars from .txt
file, all you have to do is to use >>
operator from ifstream
object, to your input variable. to check if you finish the reading, you can simply use .eof
function, of the ifstream
object.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream file("/path/to/your/file/yourfile.txt"); // From the root directory (in linux)
size_t size = 9; // Make this program to be more dynamically
char array[size][size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array[i][j] = '0';
}
}
int x, y;
char padding;
while (!file.eof()) // While there is somthing more in the file
{
file >> padding >> x >> padding >> y >> padding;
/*
read char '[', then 'x'(number), then ',', then 'y'(number), and finally ']'
*/
array[x][y] = '1'; // Place the mark
}
for (int i = 0; i < size; i++)
{
cout << i << " ";
for (int j = 0; j < size; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
cout << " ";
for (int i = 0; i < size; i++)
cout << i << " ";
cout << endl;
return 0;
}