Stack Overflow???

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;

}

int main()
{
try {
BigInt num1, num2, num3, num4, result;

num1 = "999";
num2 = "4873";
num3 = "-739";
num4 = "-9871";

BigInt num5("123456789012345678901234567890");
BigInt num6("5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555556");
BigInt num7("6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667");
BigInt num8("1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112");

//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);
*/

} catch (exception e) {
cout << "Oh crap.";
system("PAUSE");
}

system("PAUSE");
return 0;
}

//BigInt.h
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;

class BigInt {
public:
//ctors
BigInt();
BigInt(const char*);

//ctors; operators
BigInt operator=(const char*);
friend ostream &operator<<(ostream &stream, BigInt);
BigInt operator+(BigInt);
BigInt operator-(BigInt);

//methods
char* print();
BigInt add(BigInt);
BigInt subtract(BigInt);
void toTensComplement();
void fromTensComplement();
class Overflow {};

private:
int numArr[101];
};

BigInt::BigInt(const char* right) {
//Initialize
for (int i = 0; i < 100; i++) {
this->numArr[i] = 0;
}

//Pass to olOp=
this->operator=(right);
}

ostream &operator<<(ostream &stream, BigInt temp)
{
//Display useful numbers
for (int i = 0; i < 100; i++) {
stream << temp.numArr[i];
}

return stream;
}

BigInt::BigInt() {
//Initialize all values of numArr to 0
for (int i = 0; i < 100; i++) {
numArr[i] = 0;
}
}

//Overloaded = Operator
BigInt BigInt::operator=(const char* inputString) {
int origin = 100 - strlen(inputString);

//Check negative
if (inputString[origin-1] == '-') { //-83
//Change minus to a 0
this->numArr[origin-1]=0;

}

//Assign digitized Cstrings to BigInt's numArr
for (int i = origin, k = 0; i<100; i++, k++) {
char tempChar = inputString[k];
this->numArr[i] = atoi(&tempChar);
}
this->toTensComplement();
return* this;
}

char* BigInt::print(){

for (int i = 0; i<100; i++) {
cout << numArr[i];
}
cout << endl;
return "";
}

BigInt BigInt::add(BigInt right) {
//Declaration
BigInt tempInt = "0";

//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;
}

BigInt BigInt::subtract(BigInt right) {
right.toTensComplement();
return right.add(right);
}

void BigInt::toTensComplement() {
//Convert numArr to tens complement
for (int i = 0; i < 100; i++) {
this->numArr[i] = 9 - numArr[i];
}

//Add one
BigInt one = "1";
*this = this->add(one);
}

BigInt BigInt::operator+(BigInt right) {
// right.toTensComplement();
return this->add(right);
}

BigInt BigInt::operator-(BigInt right) {
return right;
}
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.)

How do you know it's a stack overflow error?
Last edited on
[code] "Please use code tags" [/code]
1
2
3
4
BigInt::BigInt (const char *) calls to
BigInt::operator= (const char *) calls to
BigInt::toTensComplement () calls to
BigInt::BigInt (const char *)


Edit: A simple backtrace.
Last edited on
Thank you ne555!
You have a problem with the way you treat negative numbers.
The code should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Overloaded = Operator
BigInt BigInt::operator=(const char* inputString) {
  int origin = 100 - strlen(inputString);
  int is_negative = 0;

  //Check negative
  if (inputString[origin-1] == '-') { //-83
    //Change minus to a 0
    this->numArr[origin-1]=0;
    is_negative = 1;
  }

  //Assign digitized Cstrings to BigInt's numArr
  for (int i = origin, k = 0; i<100; i++, k++) {
    char tempChar = inputString[k];
    this->numArr[i] = atoi(&tempChar);
  }
  if (is_negative)
    this->toTensComplement();
  return* this;
}


This will solve the code overflow.
As a freebie, and because the program now works because you are VERY lucky:
(in the above function)
1
2
3
4
5
6
7

  char zchar[2] = {0, 0};
  //Assign digitized Cstrings to BigInt's numArr
  for (int i = origin, k = 0; i<100; i++, k++) {
    zchar[0] = inputString[k];
    this->numArr[i] = atoi(zchar);
  }


The code has some more problems, but they are for you to find and solve, not showstoppers.
Topic archived. No new replies allowed.