What is wrong with this code?

Hey guys.

so I'm trying to make a program that do operation on matrices. It get's the data from a file and prints the result on another one. The maximum number of rows and/or columns of a matrix is 6.

What I did is a i got the information on the file line by line and stored them in strings (sLine[13], there are 14 values because before every matrix there is a line holding its name). Here is an example of the data on the file:

input_M1
1,2,4,5,6,7
2,1,1,2,3,4
5,8,4,2,7,4
6,3,6,9,6,5
5,8,2,6,4,3
8,5,1,4,2,5
input_M2
3,1,7,9,6,2
4,6,8,5,2,4
5,4,8,2,6,4
8,5,2,1,4,5
5,2,5,4,8,6
8,5,2,1,4,5

after I stored every line on a string. I want to store each number on an integer so I can do the matrices operation (it is mostly matrices multiplication).

So here is the problem.. after the loop is finished I tried to print out all the numbers I had separately to make sure I had them all correctly, and this was mostly the case, except for the last number in each row. The last number in each row was rewritten (I'll tell you why I think it was rewritten) by the first number of the next row. Now this doesn't mean the first number of the next row was rewritten by the number after it and so on. It was just like the first number of each row was stored twice, once as where it should be, and once as the last number of the row before it.

This is an example of how I would get the data from the first matrix:

1 2 4 5 6 2
2 1 1 2 3 5
5 8 4 2 7 6
6 3 6 9 6 5
5 8 2 6 4 8
8 5 1 4 2 5

Now you might notice that the last row came perfectly fine. I don't know why.

So now I'll tell you why I think it was re written. I went ahead and put a cout code inside the loop ( you can find it in the code below followed by a comment) and I ended up getting the values like they should be.

so here is the code. I hope you can help me figure out what I got wrong. Thanks!

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
struct rows {
		struct columns
{
	int c[5];
}r[5];
} m1,m2,mr;

  for (j=1;j<=m1max;j++) //m1max is the last line of the first matrix.
{
	for(y=0;y<6;y++)
	{
		k=sLine[j].find(",");
		if(k==-1) // I figured that when the program can't find the comma, it assigns "-1" to k.
	{
		m1.r[j-1].c[y]=atoi(sLine[j].c_str());
		cout << m1.r[j-1].c[y] << endl; // if I delete this and try to print out the numbers of
//the matrix after this nested loop. the last number in each row will be overwritten by the first number of the next row
		m1c=y+1;
		if(m1c>6)
		{
			cout << "First matrix has more than 6 columns. Operation cannot be done. closing the program..." << endl;
	system("PAUSE");
		}
		break;
	}
	m1.r[j-1].c[y]=atoi(sLine[j].substr(0, k).c_str());
	sLine[j]=sLine[j].substr(sLine[j].find(",")+1); 
	cout << m1.r[j-1].c[y] << endl; // if I delete this and try to print out the numbers of
//the matrix after this nested loop. the last number in each row will be overwritten by the first number of the next row
	}
	
}
Lines 4 and 5. Your "matrix" is 5x5. Your code writes 6x6 values to it.
Thanks man!

That fixed it; however, I don't understand why. Could you explain?
You declared a array that can hold 5 elements and were trying to put 6 elements in it.
The compiler will have allocated enough space to keep 5 elements though the compiler wouldn't complain if you put extra elements there is no space in memory reserved for it.

This means that it can corrupt other data used by your program or can crash it. Basically the behavior is undefined, and it is not a good practice to do this.
Topic archived. No new replies allowed.