Why are Marks not copied in my array?(streams)

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string>
using namespace 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;
}
Last edited on
Think about how this loop knows when to stop:
1
2
	while (source >> x >> y)
		arr[x][y] = 'B';

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???)
Last edited on
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
Last edited on
Topic archived. No new replies allowed.