Issues with file.get(x) and arrays"

Let's say I have a file with the following store:

AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
BBBBBBBBBAAAABBBBBBB
BBBBBBBBBAAAABBBBBBB


And I use ifstream file(....) with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
                for(int j = 0; j < 20; j++)
                {    
                        for(int i = 0; i < 20; i++)
                        {
                                file.get(x);
	        	        origCanvas[i][j] = x;
	        	        cout << x;

	        	        if(j == 0 && i == 0)
	        	        cout << "X" << endl;

	        	}


Instead of getting the same printout as what I wrote to the file... I get:

AXAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
BBBBBBBBBAAAABBBBBBB
BBBBBBBBBAAAABBBBBBB


and if:
1
2
	        	        if(j == 1 && i == 0)
	        	        cout << "y" << endl;


then I get:
AAAAAAAAAAAAAAAAAAAA
yAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBAAAABBBBBBB
BBBBBBBBBAAAABBBBBBB




Why is that? By the way... I outputed the x and y just to find out were the problem is.
Last edited on
You're getting the extra letter because you're printing an extra letter.

1
2
	        	        if(j == 0 && i == 0)
	        	        cout << "X" << endl;  // this is printing the extra X 


if you don't want that X to be printed, then don't print it.


....

...

wait a minute...

this isn't ANOTHER console game attempt... is it?
No, it isn't and.... I added that if statement to show where the problem is. In the first example where I added that if j ==0 and i ==0 it's suppose to print out an x after the first letter is pulled... hence the location origCanvas[0][0]. So in this case the X printed out in the right place.

In the example where the if statement is j == 1 and j ==0 (location origCanvas[1][0])... instead of printing out the A first and then the X (like in location (0,0) it prints out before. The further I go down the line the more offset it gets even when I hold x constant (were (x,y) x=0).
I'm thinking that maybe hitting the "enter button" at the end of each line in the document has something to do with it.
I did a little more test and added a coordinate system it looks roughly like this:

so it's suppose to print out (x,y) A| per loop.

(0,0)A| (1,0)A| (2,0)A| (3,0)A|.......(17,0)A| (18,0)A| (19,0)A| (0,1)
| (1,1)A| (2,1)A| (3,1)A| (4,1)A|.....(17,1)A| (18,1)A| (19,1)A| (0,2)A| (1,2)
Topic archived. No new replies allowed.