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.
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)
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.
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
usingnamespace std;
double* multiplyPolynomials( constdouble* const p,constdouble* const q,int np, int nq){
double* result = newdouble[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 = newdouble[sizep];
double* arrq = newdouble[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
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 - -
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.