I am trying to copy characters from a file into a 2d array.
Here is a screenshot of my notepad http://prntscr.com/jgexjx
My problem is my Marks aren't replacing the old characters in the array.(look at my comment where I said it's not working)
here is the output http://prntscr.com/jgeyb2... Boxes ('B' characters) and initial Position('I' character) are stored correctly but it seems there is a problem with the 'M' characters getting stored
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main() {
char arr[8][8];
int x, y;
string temp, Hello;
ifstream source;
source.open("Myfile.txt");
if (!source) {
cerr << "error!!\n";
//exit(1);
}
//store characters in text to array
string line;
for (int r = 0; r < 8; r++) {
getline(source, line);
for (int c = 0; c < 8; c++)
arr[r][c] = line[c];
}
//ignore string after
source >> temp;
//replace characters at indexes x and y in array with charachter 'B'
while (source >> x >> y)
arr[x][y] = 'B';
//ignore string after
source >> temp;
//replace characters at indexes x and y in array with charachter 'M' (NOT WORKING)
while (source >> x >> y)
arr[x][y] = 'M';
//ignore string after
source >> temp;
//replace character at indexes x and y in array with charachter 'P'
source >> x >> y;
arr[x][y] = 'P';
source.close();
//print my final array
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
cout << arr[r][c];
cout << endl;
}
source.close();
system("pause");
return 0;
}
It is trying to read two integers, so it will stop when it either hits the end of the file or something that it can't interpret as an integer. In your file, that's the 'M' in "MarksPositions:". So the file pointer will be pointing to the M. However, the stream will be in an failure state since it just failed to read something. To reset its state use source.clear() after each of those loops.
BTW, posting the actual text of the input file (in output tags) is far better than a picture. We can't copy/paste from a picture.
It is trying to read two integers, so it will stop when it either hits the end of the file or something that it can't interpret as an integer. In your file, that's the 'M' in "MarksPositions:". So the file pointer will be pointing to the M. However, the stream will be in an failure state since it just failed to read something. To reset its state use source.clear() after each of those loops.
BTW, posting the actual text of the input file (in output tags) is far better than a picture. We can't copy/paste from a picture.
(Why are you using a different username???)
You're a life savior !
Hahaha about the username thingy, me and my friend are working on the same project so it's no surprise you may find similar codes xD