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;
}
The code is not retroactive. It executes sequentially from top to bottom.
Thank you first. But I am sorry, I don't get it.
Is that the sequence of the code not good?
BTW, I solved the "-1" problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
	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]; //a zero size array ¡¿?!

//...
	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]; //arrp still is a zero size array ¡¿?!
	}
I still do not get it....
should I initialize like this??
int sizep;
int sizeq;
You should get the size, and then construct the array.
1
2
cin >> sizep;
arrp = new double[sizep];
In order to prevent memory leaks and double deletes I suggest you to use std::vector instead

duplicated thread: http://cplusplus.com/forum/beginner/62592/
Last edited on
Declare your arrays after you have their size from the user.
But make sure you don't have invalid value for size like -1 or 0.

1
2
3
4
5
6
7
8
9
10
	while(sizep != -1){
	cout << "Number of terms for the first polynomial p: ";
	cin >> sizep;
	double* arrp = new double[sizep];

//...
	cout << "Number of terms for the first polynomial q: ";
	cin >> sizeq;
	double* arrq = new double[sizeq];


thank you!
But if I put the array like below, initial
double* arrp;
double* arrq; <~ these two in the top

1
2
cin >> sizep;
arrp = new double[sizep];


the problem is still unsolved.

And if I put double* arrp = new double[sizep]; below cin >> sizep; the problem is still unsolved.
The message is the same
Thank you all of you!
you guys have helped me a lot !
Now I can run the programme properly!!!!!!!
Thanks !! :) !!
Topic archived. No new replies allowed.