I am very new in C++ programing. I tried to write a C++ code, where a matrix from an input file will be created for a given condition(the condition is written as afunction). After creating the first row of the matrix I get the folloing message:
Filename.exe has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.
My input file is as follows (matrix.txt):
1 2 3 4 5 6 7 8 9 10 11 12 13
This is a line, below is another line with numbers
0.15 10.1 15.001
*M
2.5 3.01 4.11
*M
6.16 7.17 8.01
*M
10.16 11.133 12.12
*M
14.17 15.110 16.16
Tip: Learn how to debug to catch these types of problem and save yourself countless of days in the future.
Problem:
The vector "matr" is of size 7.
1 2 3 4 5 6 7 8
for (int i = 0; i<n; i++)
{
for (int j = 0; j<m; j++)
{
cout << matr[i][j] << " ";
}
cout << '\n';
}
the problem is at the second loop. When i = 1. If you debug the code you find out that matr[0] is of size 3, which is what you expect. So is matr[2-6], But now matr[1]. matr[1] is of size 0, which makes the program crash at matr[1][j].
Thank you very much, now it works. When I had my read function inside the int main (), it did work and the rows with *M were not taking into the matrix size (the size was 4x3), but once I tried to rearrange the code and creat the function it took also the rows with *M into account.
As I am very new in programing and it may sound stupid, could you suggest some tutorails where I can learn debugging.
One more question. Suppose my input file continues with strings as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This is a line, below is another line with numbers
0.15 10.1
*M
2.5 3.01 4.11
*M
6.16 7.17 8.01
*M
10.16 11.133 12.12
*M
14.17 15.110 16.16
Reading should stop in this line
another line
another line
How can I read lines after *M only with numbers? Because currently my code reads all the lines after *M. I tried break option with if statement but it didnt work.