I coded so nice and clean, but I got a stack overflow error and tried to fix it... for five hours. Now my code is a mess and the stack overflow is still rubbing its ugly face in mine. Can someone please tell me what the flip is wrong with my code here? My professor is offended when we ask for help...
//BigInt.cpp
//Just a calculator that can handle 100 digits
#include "BigInt.h"
using namespace std;
void displayResult(BigInt& num) {
cout << "Result is: " << num << endl;
//Test some addition problems
result = num1.add(num2);
displayResult(result);
cout << "Correct Answer Was = 5872" << endl;
result = num3.add(num4);
displayResult(result);
cout << "Correct Answer Was = -10610" << endl;
result = num8 + num8;
displayResult(result);
cout << "Correct Answer Was = 22222222...2224" << endl;
try{
result = num6 + num7;
displayResult(result);
} catch(BigInt::Overflow& errorObj) {
cout << "The result was too big!" << endl;
}
//Some subtraction problems
result = num1.subtract(num2);
displayResult(result);
result = num2.subtract(num1);
displayResult(result);
result = num4.subtract(num3);
displayResult(result);
result = num5.subtract(num6);
displayResult(result);
result = num6 - num5;
displayResult(result);
//For homework #2
/*
//Some multiplication problems
result = num1.mult(num2);
displayResult(result);
result = num1.mult(num3);
displayResult(result);
result = num4 * num3;
displayResult(result);
try{
result = num5 * num5;
displayResult(result);
} catch(BigInt::Overflow& errorObj) {
cout << "The result was too big!" << endl;
}
try{
result = num6 * num3;
displayResult(result);
} catch(BigInt::Overflow& errorObj) {
cout << "The result was too big!" << endl;
}
//Some division problems
result = num2.divide(num1);
displayResult(result);
result = num5.divide(num3);
displayResult(result);
result = num6 / num4;
displayResult(result);
result = num4 / num3;
displayResult(result);
result = num6 / num3;
displayResult(result);
*/
//Loop from right to left
for (int i = 100; i>0; i--) {
tempInt.numArr[i] += this->numArr[i] + right.numArr[i];
if (tempInt.numArr[i] > 9) {
//Reduce by 10
tempInt.numArr[i] -= 10;
tempInt.numArr[i-1] = tempInt.numArr[i-1]+1;
}
}
return tempInt;
}
Yea, you might want to put that all in [ code ] and [ /code ] tags...Stack overflows often happen when you have infinite recursion. Another problem could be you're trying to allocate to much data within the scope of a function (like a HUGE array.)