Program compiles but won't Run? (Dev C++)

Okay this is a first for me. My code compiles absolutely fine with no errors what so ever. But when I click "Run" or "Compile & Run", the little command window pops up but the output never appears? The command window tries to load the code for about 5 seconds and then it gives up, when usually it takes about 1 second to see my output.

After it tries to load, the command window stays scrolled down all the way at the bottom. Usually the code appears at the very beginning of the command window, so when I tried to scroll up, the side bar immediately pulls itself down.

I have made 9 different programs over the past 5 months and every single one of them runs fine except this one.

What I am doing in my code, is creating a class. This class is using methods to add, subtract, and multiply matrices. Assuming my code is fine, why is Dev C++ doing this?

I'd post my code, but it compiles without a hitch and it's 300+ lines of code. I'm sure nobody wants to read that much into this.

Does anyone know why this happening?

P.S. I don't even get the "Press any key to continue...." that usually appears after your output in the command prompt window, even after waiting five minutes.

From what you're describing, the window is not struggling to load the program. In fact, it is in an almost infinite loop, printing new line after new line. After it stopped, the program was probably doing something else in the background that proved to be sluggish.
Check all of your loops thoroughly. The fault could be as simple as a typo or a missing line that is causing the problem.
I thought it was in an infinite loop as well, but I double checked all my loops and everything seems fine. I think I need a fresh pair of eyes. Maybe you could take a look at what I have?

These are my list of methods that use for loops. The only loops I use in my program.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void Matrix::print()
{
	for ( int i = 0; i < row; i++)
	{
		for (int sub = 0; sub < column; sub++)
		{
			cout << array[i][sub] << "\t";
		}
	cout << endl;
	}
}

Matrix Matrix::add(Matrix anotherMatrix)
{
	if(row != anotherMatrix.row || column != anotherMatrix.column)
		{
			cout << "Addition Error:" << endl;
			cout << "\tThe two matrices cannot be added together because they are different sizes. The result will be equal to the current instance." << endl;
			return *this;
		}
	
	Matrix neo(row, column);
	
	for (int i = 0; i < row; i++)
	{
		for (int sub = 0; sub < column; sub++)
		{
			neo.array[i][sub] = array[i][sub] + anotherMatrix.array[i][sub];
		}
	}
	
	return neo;
}

Matrix Matrix::subtract(Matrix anotherMatrix)
{
	if(row != anotherMatrix.row || column != anotherMatrix.column)
		{
			cout <<"Subtraction Error:" << endl;
			cout << "\tThe two matrices cannot be subtracted because they are different sizes. The result will be equal to the current instance." << endl;
			return *this;
		}
	
	Matrix neo(row, column);
	
	for (int i = 0; i < row; i++)
	{
		for (int sub = 0; sub < column; sub++)
		{
			neo.array[i][sub] = array[i][sub] - anotherMatrix.array[i][sub];
		}
	}
	
	return neo;
}

Matrix Matrix::multiply(int scalar)
{
	Matrix neo(row, column);
	
	for (int i = 0; i < row; i++)
	{
		for (int sub = 0; sub < column; sub++)
		{
			neo.array[i][sub] = array[i][sub] * scalar;
		}
	}
	
	return neo;
}

Matrix Matrix::multiply(Matrix anotherMatrix)
{
	if (column != anotherMatrix.row)
		{
			cout << "Multiplication Error:" << endl;
			cout << "\tThe two matrices cannot be added together because they are different sizes. The result will be equal to the current instance." << endl;
				return *this;
		}
		
	Matrix neo(row, anotherMatrix.column);
	
	int localRow, localColumn, currentRow, currentColumn, anotherRow, anotherColumn, dotProduct;
	
	for (localRow = 0; localRow < neo.row; localRow++)
	{
		currentRow = localRow;
		for (localColumn = 0; localColumn < neo.column; localColumn++)
		{
			anotherColumn = localColumn;
			anotherRow = 0;
			dotProduct = 0;
			for (currentColumn = 0; currentColumn < column; currentColumn++)
			{
				dotProduct += anotherMatrix.array[anotherRow][anotherColumn]*array[currentRow][currentColumn];
				anotherRow++;
			}
			neo.array[localRow][localColumn] = dotProduct;
		}
	}


Here are my constructors, so you can better understand the Matrix class.

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
Matrix::Matrix()
{
	int array[sizeRow][sizeColumn];
	
	for (int i = 0; i < sizeRow; i++)
	{
		for ( int sub = 0; sub < sizeColumn; sub++)
		{
			array[i][sub] = 0;
		}
	}
}

Matrix::Matrix(int x, int y)
{
	if (x <= sizeRow)
		row = x;
	else 
		row = sizeRow;
		
	if (y <= sizeColumn)
		column = y;
	else 
		column = sizeColumn;
}

Matrix::Matrix(int ar[][5], int x, int y)
{
	if (x <= sizeRow)
		row = x;
	else 
		row = sizeRow;
		
	if (y <= sizeColumn)
		column = y;
	else 
		column = sizeColumn;
		
	ar[row][column];
}


If you want the whole program, I'll be more than willing to post it.
I wish I could help you, I really do. But I can't spot the problem either. the only things I could spot are the following (but these are more for optimizing):

1)You declare new variables called currentRow and anotherColumn, assigning them the values of their respective iterators before the next inner for loop. I don't see why you need separate variables since you could just use the iterator variables themselves. In fact, on line 98, you do just so while on line 95, you obfuscate what variables you are actually using.

2) What is the purpose of line 39 in the second code block?
ar[row][column];
You are accessing a value pointed by ar at indices row and column (which are probably out-of-bounds) and do nothing with it.

Posting your entire program (considering how long it probably is) should be your last resort. Hopefully another programmer on this forum has a sharper eye than mine.
It could be really hard to spot a runtime error by looking at the code - so investigate using the debugger instead - that is by far the easiest way.

Topic archived. No new replies allowed.