Reputation: 43
I'm doing a bigint project and I am stumped as to why my addition operator isn't working properly on a test case.
I'm excluding the .h file because its probably unnecessary.
#include "bigint.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cassert>
bigint::bigint()
{ //Default constructor that sets digit to ZERO
for (int i = 0; i < MAX; i++)
{
digits[i] = 0;
}
}
bigint::bigint(int n)
{
for(int i = 0; i < MAX; ++i) //Sets the digit to ZERO
digits[i] = 0;
for (int i = 0; n != 0 ; ++i)
{
digits[i] = (n % 10); //
n = n / 10;
}
}
bigint::bigint(const char new_digits[])
{
int null = 0;
int temp = 0;
for(int i = 0; i < MAX; ++i)
{
digits[i] = 0;
}
while(new_digits[null] != '\0')
++null;
--null;
temp = null;
for(int j = 0; j < MAX && temp >= 0; ++j)
{
digits[j] = new_digits[temp] - '0';
temp -= 1;
}
}
bool bigint::operator==(const bigint& equal) const
{
int i = 0;
while(i < MAX)
{
if(digits[i] != equal.digits[i])
{
return false;
}
++i;
}
return true;
}
std::ostream& operator<<(std::ostream& output, const bigint& source)
{
int sub1 = MAX - 1; //subtracts 1 from the maximum size
while(source.digits[sub1] == 0)
{
--sub1; //EMPTY
}
while(sub1 > -1)
{
output << source.digits[sub1];
--sub1;
}
std::cout << std:: endl;
return output;
}
std::istream& operator>>(std::istream& in, bigint& source)
{
char getdata[MAX];
char user_input;
int i = 0;
in.get(user_input);
while(!in.eof() && user_input != ';')
{
in.get(user_input);
source.digits[i] = user_input;
++i;
}
source = bigint(getdata);
return in;
}
char bigint::operator[](const int i)
{
return digits[i];
}
bigint bigint::operator+(const bigint rhs)
{
bigint result;
int i = 0;
for( ; i < MAX; ++i)
{
if((digits[i] + rhs.digits[i]) > 9)
{
digits[i+1] = digits[i+1] + 1 ;
}
result.digits[i] = (digits[i] + rhs.digits[i]);
result.digits[i] = result.digits[i] % 10;
}
return result;
}
int main()
{
// Setup fixture
bigint left("1");
bigint right("9");
bigint result;
// Test
result = (left + right);
Verify
assert(left == "1");
assert(right == "9");
assert(result == "10");
}
In this test case the program aborts at assert(result == "10");
but if I have the same test case except with assert(result == 10); the program runs.
Can anyone say why?
Upvotes: 1
Views: 3351
Reputation: 98469
First off, you should implement bigint::operator=(const bigint&)
, the assignment operator.
Now, in operator+
, you're altering the contents of the left-hand side object, in this code:
if((digits[i] + rhs.digits[i]) > 9)
{
digits[i+1] = digits[i+1] + 1 ;
}
That's not good. For example, if you ran this code:
bigint x("5");
bigint y("6");
x+y;
x+y;
You would end up with x
being 17.
Next, you're passing by value for the bigint::operator
arguments where you should probably pass by reference (&
).
Finally, your indentation here is actively malicious:
while(new_digits[null] != '\0')
++null;
--null;
What is in the loop body here? That's right, not the third line. Please don't indent code like that, it makes kittens cry. Programming kittens, at least.
NB: I don't see any dynamic memory allocation code here, which means digits
is probably a statically-sized array. Make sure it's big enough if you're going to do that and be aware you'll break if its size is exceeded.
Upvotes: 3