I cannot run properly my C++ programme

When I enter all the number in my C++ programme. Right before the output print out , the below statement will be show. A dbgheap code will be shown also and there is a lock beside the file name. And these are my code.
And I wanna ask how to end my programme when user enter -1 in the "Number of terms for the first polynomial:" statement I used the while/do-while below but It cannot run properly also.
Thanks!!!
Windows has triggered a breakpoint in <Filename>.exe.

This may be due to a corruption of the heap, which indicates a bug in Lab5c2.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Lab5c2.exe has focus.

The output window may have more diagnostic information.
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
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>

using namespace std;

double* multiplyPolynomials( const double* const p,const double* const q,int np, int nq){
	 double * output = new double[np+nq-1];
        for (int i=0;i<np+nq-1;i++)
                output[i] = 0.0;
 
        for (int i=0;i<np;i++) 
                for (int j=i;j<i+nq+1;j++) 
                        output[j] = output[i] + p[i]*q[j];
        return output;
 
}
int main()
{   

	int counter=0;
	int roundn = 1;
	int sizep = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	int sizeq = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	double* arrp = new double[sizep];
	double* arrq = new double[sizeq];


	cout << "Polynomial Solver"<<endl;
	cout << "-----------------"<<endl;

	
	while(sizep != -1){
	cout << "Number of terms for the first polynomial p: ";
	cin >> sizep;

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	for(int i= 0; i < sizep; i++){
		
		cin	 >> arrp[i];
		
	}
	cout << "Number of terms for the first polynomial q: ";
	cin >> sizeq;

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	for(int i= 0; i < sizeq; i++){
		
		cin	 >> arrq[i];
		
	}
	
	
	 double* output = multiplyPolynomials(arrp, arrq, sizep, sizeq);
	 cout << "Result: ";

	 for (int i=0;i<sizep+sizeq-1;i++) {
                if (i!=0) {
                        cout << ", " << output[i] << " ";
                        cout << "x" << i;
                }       
                else
                        output[i];
        
        }

	
        delete[] output;
        system("pause");        
        return 0;
	cout <<"Thanks for using our program!";

	delete[]arrp;
	delete[]arrq;
	
	}
	
	// Hold the command window
	system("pause");
	return 0;
}
you must not call line 57 - 71 when sizep and/or sizeq is negative. This is the time to write your first if ;)

The lines after 74 (return 0) - 79 are never executed since the function exits due to the return.

EDIT: Line 26 and 27 don't make sense there. Put them directly before line 57 (within your new if clause). And also the matching deletes before line 68 (as well as the output delete)
Last edited on
Thank you first!! I add a if then if sizep<0 the statement will break Here is my new code.
I don't get how to solve about your second statement . ><
But it still cannot run properly.


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
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>

using namespace std;

double* multiplyPolynomials( const double* const p,const double* const q,int np, int nq){
	double*	result = new double[nq + np - 1];

for (int i = 0; i < np + nq; i++) result [i] = 0.0f;

for (int i = 0; i < np; i++) {
for (int j = 0; j < nq; j++) {
result [i + j] += p[i] * q[j];
}
}

return result;
 
}
int main()
{   

	int counter=0;
	int roundn = 1;
	int sizep = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	int sizeq = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	double* arrp = new double[sizep];
	double* arrq = new double[sizeq];


	cout << "Polynomial Solver"<<endl;
	cout << "-----------------"<<endl;

	
	while(true){

		
	cout << "Number of terms for the first polynomial p: ";
	cin >> sizep;

	if (sizep < 0){
		cout << "Thanks for using our program!" << endl;
		break;}
	 else {

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizep; i++){
		
		cin	 >> arrp[i];
		
	}
	

	cout << "Number of terms for the first polynomial q: ";
	cin >> sizeq;

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizeq; i++){
		
		cin	 >> arrq[i];
	}	
	
	
	
	 
	 double* output = multiplyPolynomials(arrp, arrq, sizep, sizeq);
	 cout << "Result: ";
	
	 for (int i=0;i<sizep+sizeq-1;i++) {
                if (i!=0) {
                        cout << ", " << output[i] << " ";
                        cout << "x" << i;
                }       
                else
                        output[i];
	 
      } 

	 
	 delete[] output;}
        system("pause");        
        return 0;
	

	delete[]arrp;
	delete[]arrq;
	 
	 }
	
	// Hold the command window
	system("pause");
	return 0;
}
On line 27/28 you initialize sizep/q with 0. That's ok. But then on line 29/30 you create arrays of size 0! That causes the crash because you permanently write something beyound the bounds of those arrays.

Therefore create the array when sizep/q do actually have valid values. That is right before line 72. And when you're done with arrp/q you should delete them. That is right after line 83.

Line 88 ends main(), right? How could line 91/92 be executed? The compiler should complain about unreachable code
I am sorry, is that what you mean, but it is the same :(
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
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>

using namespace std;

double* multiplyPolynomials( const double* const p,const double* const q,int np, int nq){
	double*	result = new double[nq + np - 1];

	for (int i = 0; i < np + nq; i++) result [i] = 0.0f;

	for (int i = 0; i < np; i++) {
	for (int j = 0; j < nq; j++) {
	result [i + j] += p[i] * q[j];
	}
}

return result;
 
}
int main()
{   

	int sizep = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	int sizeq = 0; // intialize to zero so if cin fails, it's within invalid bounds.
		double* arrp;
		double* arrq;



	cout << "Polynomial Solver"<<endl;
	cout << "-----------------"<<endl;

	
	while(true){

		
	cout << "Number of terms for the first polynomial p: ";
	cin >> sizep;
	arrp= new double[sizep];

	if (sizep < 0){
		cout << "Thanks for using our program!" << endl;
		delete[]arrp;
		break;}
	 else {

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizep; i++){
		
		cin	 >> arrp[i];
		
	}
	

	cout << "Number of terms for the first polynomial q: ";
	cin >> sizeq;
	arrq= new double[sizeq];
	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizeq; i++){
		
		cin	 >> arrq[i];
	}	

	 
	 double* output = multiplyPolynomials(arrp, arrq, sizep, sizeq);
	 cout << "Result: ";
	
	for (int i = 0; i < sizep + sizeq; i++) {
	if (output[i] != 0) {
	if (i == 0) cout << output [i] << ", ";
	else cout << output [i] << " x" << i << ", ";
	}
	}
	cout << endl << endl;

	 delete[]arrp;
	 delete[]arrq;
	 delete[] output;}
	 }
	
	// Hold the command window
	system("pause");
	return 0;
}
Hm, yes. On line 9 it's nq + np - 1. That means the array is 1 to short.

The way you place the } make it very hard to see which block ends
Yes, I am sorry- -
It's to hard to read.
I moved some of them.
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
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>

using namespace std;

double* multiplyPolynomials( const double* const p,const double* const q,int np, int nq){
	double*	result = new double[nq + np - 1];

	for (int i = 0; i < np + nq; i++) result [i] = 0.0f;

	for (int i = 0; i < np; i++) {
	for (int j = 0; j < nq; j++) {
	result [i + j] += p[i] * q[j];
	}
}

return result;
 
}
int main()
{   

	int sizep = 0; // intialize to zero so if cin fails, it's within invalid bounds.
	int sizeq = 0; // intialize to zero so if cin fails, it's within invalid bounds.
		double* arrp;
		double* arrq;



	cout << "Polynomial Solver"<<endl;
	cout << "-----------------"<<endl;

	
	while(true){

		
	cout << "Number of terms for the first polynomial p: ";
	cin >> sizep;
	arrp= new double[sizep];

	if (sizep < 0)
	{
		cout << "Thanks for using our program!" << endl;
		delete[]arrp;
		break;
	}
	 else 
	 {

	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizep; i++)
	{
		
		cin	 >> arrp[i];
		
	}
	

	cout << "Number of terms for the first polynomial q: ";
	cin >> sizeq;
	arrq= new double[sizeq];
	cout << "Terms (from lowest order term to the highest):";
	// get user guessing
	
	for(int i= 0; i < sizeq; i++)
	{
		
		cin	 >> arrq[i];
	}	

	 
	 double* output = multiplyPolynomials(arrp, arrq, sizep, sizeq);
	 cout << "Result: ";
	
	for (int i = 0; i < sizep + sizeq; i++) 
	{
	if (output[i] != 0) 
	{
	if (i == 0) cout << output [i] << ", ";
	else cout << output [i] << " x" << i << ", ";
	}
	}
	cout << endl << endl;

	 delete[]arrp;
	 delete[]arrq;
	 delete[] output;
	}
	}
	
	// Hold the command window
	system("pause");
	return 0;
}


the { down the else is close right below delete[] output;
So place line 41 after line 44

and

line 9: double* result = new double[nq + np]; // not - 1

that's supposed to be it. if there's no problem with the scope i.e. { }
thanks! done the above job..
but there is a failure- -
Run-Time Check Failure #3 - The variable 'arrp' is being used without being initialized.
in line 40. I am sorry - -
is it because if it is in line 44 (arrp= new double[sizep];) then arrp is only in the if statement??
wait - not line 44. put it in the else branch after line 50. That's the place where 'sizep' is correct.

I'm used to the positive logic. That means do something when it's ok. In your case: if (sizep > 0) which would the better case since you don't want 0 for sizep.
Thank you coder777, you have helped me a lot lot lot lot lot !
Now I can run the programme properly!!!!
Thanks !! :) !
Topic archived. No new replies allowed.