Hi guys,
thanks for the comments, @keskiverto, the elements in array p are obtained via a string length ((This program is meant to get a big number, and split it into an int array dynamically))
To be honest, when I posted this, it was around 4-5am and I pulled an all nighter so looking back at the code, it was pretty bad! I did optimize it, and fix it up, but more problems arise and I don't understand why it's going on. They're really weird, and it being Friday, I can't contact my professor for help, so I would highly appreciate it from you guys just to kick me to the right direction.
Problem 1) My destructor, when I leave it to delete [] p;, it triggers a breakpoint, which then eventually just crashes my program without even doing any addition. Yet if i //delete []p, it will do the addition, but then eventually just crash the program.
Problem 2) my operator+, it now will add arrays fully, which is good! It didn't before, but it returns values in a weird way.
Example:
Large1 would be 111111111
Large2 would be 222222222
so then Large3 would be 333333333
but instead, I get Large1 & Large3 being 333333333
Problem 3) operator+ again, when i create a new object, result, and try to do all the functions I need on it, the program will crash. Yet if I invoke the copy constructor by result(*this), it will return the values, but in the way I mentioned in problem 2.
That seems to be it, I've went over all my functions, looking to see if I did any small errors, and I can't find any at all!
new header:
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
|
#ifndef LARGEINT_H
#define LARGEINT_H
#include <iostream>
#include <string>
using namespace std;
class LargeInt
{
friend istream& operator>> (istream& f, LargeInt &a);
friend ostream& operator<< (ostream& f, const LargeInt &a);
public:
LargeInt();
~LargeInt();
LargeInt(const LargeInt &a);
int maxSize;
int length;
int * p;
string bigInt;
LargeInt operator+ (const LargeInt num2) const;
LargeInt& operator= (const LargeInt& num2);
};
#endif
|
new cpp file:
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
|
#include "LargeInt.h"
LargeInt::LargeInt()
{
maxSize = 100;
length = 0;
p = new int[];
bigInt = "NULL";
}
LargeInt::~LargeInt()
{
cout << "DESTRUCTOR CALLED" << endl;
delete [] p;
}
LargeInt::LargeInt(const LargeInt &a)
{
maxSize = a.maxSize;
length = a.length;
p = a.p;
bigInt = a.bigInt;
}
istream& operator>>(istream& f, LargeInt &a)
{
f >> a.bigInt;
a.length = a.bigInt.length();
for (int i = 0; i < a.length; i++)
a.p[i] = a.bigInt[i] - 48;
return f;
}
ostream& operator<<(ostream& f, LargeInt &a)
{
for (int i = 0; i < a.length; i++)
f << a.p[i];
return f;
}
LargeInt LargeInt::operator+(const LargeInt num2) const
{
LargeInt result(*this); // If I don't "copy" *this, it won't return values to the new object, don't understand why..
int carry = 0;
int i = length;
int j = num2.length;
for (i, j; i >= 0 || j >= 0; i--, j--)
{
result.p[i] = p[i] + num2.p[j] + carry;
result.bigInt[i] = result.p[i];
/* if (carry == 1)
carry--;
else if (this->p[i] > 10)
{
result.p[i] = p[i] / 10;
carry++;
}*/
}
return result;
}
LargeInt& LargeInt::operator= (const LargeInt& num2)
{
if (this != &num2)
{
maxSize = num2.maxSize;
length = num2.length;
p = num2.p;
bigInt = num2.bigInt;
}
return *this;
}
int main()
{
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
LargeInt large1, large2, large3;
cout << "Enter your number.\n";
cin >> large1;
cout << "Enter your number.\n";
cin >> large2;
cout << "\n";
large3 = large1 + large2;
cout << "---- FIRST # ";
cout << large1;
cout << "\n---- SECOND # ";
cout << large2;
cout << "\n---- THIRD # ";
cout << large3;
cout << "\n---- THIRD LENGTH ";
cout << large3.length;
cout << "\n";
system("pause");
return 0;
}
|