"File name".exe has stopped working

Dear All,

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



And my code is as follows:

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
51
52
53
54
55
// Reading the input file.
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <limits>
#include <iterator>
using namespace std;

vector<vector<double> > read(ifstream& fil, string sec,string fir)
{
    string lines;
    vector<vector<double> > data;

        if (fir == sec)
        {
            while (getline(fil,lines))
            {
                //istringstream is(lines);
                stringstream is(lines);
                data.push_back(vector<double>(istream_iterator<double>(is),
                                              istream_iterator<double>()));
            }
        }
    return data;
}

int main()
{
    int n, m;
    ifstream pa("matrix.txt");

    vector<vector<double> > matr;
    string first, ba("*M");

    while (pa>>first)
    {
        pa.ignore(numeric_limits <streamsize>::max(),'\n');
        matr = read(pa,ba,first);
    }
    n = matr.size();
    m = matr[0].size();
    for ( int i = 0; i<n; i++)
    {
        for (int j = 0; j<m; j++)
        {
            cout << matr[i][j]<< "  ";
        }
        cout << '\n';
    }
    cout << n << '\n';
    cout << m;
    return 0;
}



As mentioned I get the first row (2.5 3.01 4.11), then the program stops. I use Code::Blocks 16.01. Any suggestions will be much appreciated!

With friendly greetings,
qsqais

Last edited on
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].
Dear TarikNeaj,

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.

Kind regards,
qsqais

ould you suggest some tutorails where I can learn debugging.

There is no "definitive" way of learning how to debug proparly, read here - http://stackoverflow.com/questions/3974230/how-to-learn-debugging

Although this is not c++, it's c#. It should be fairly easy to follow -
https://www.youtube.com/playlist?list=PLHJE4y54mpC4lnCrwbEssAf9e-A9U4el4

Last edited on
Thanks alot once again!!

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