Hey guys so I'm having an issue with my program. Some information about my program it stores a const char* str converts it to int which is in ascii then it runs a test on the number and converts it back to a string. Then it will also add these numbers up for some reason I am getting passed my first two tests but when I work on the addition one I'm getting the wrong negative number I will post my code below, test 3 is the one giving the wrong output. When I print it out I see the numbers there but something is happening maybe where the -3 appears before the number for example 937-300000000 etc. If you could help that would be greatly appreciated, I've been staring at it for awhile maybe another input will help. Thanks in advanced.
#include <iostream>
using namespace std;
class BigInt
{
public:
BigInt() {}
BigInt(const char*);
BigInt add(const BigInt&);
BigInt operator+(const BigInt&);
string convertToString();
private:
static const int NUM_DIGITS = 100;
int numArr[NUM_DIGITS];
void tensComplement();
};
//A constructor which accepts a C string
BigInt::BigInt(const char* str) {
// TODO: CONVERT C-STRING TO BIGINT
int len = strlen(str) - 1;
int zero = NUM_DIGITS - 1;
for (int i = 0; i < NUM_DIGITS; i++){
numArr[i] = 48;
}
for (int i = len; i >= 0; i--){
numArr[zero] = str[i];
zero--;
}
}
BigInt BigInt::add(const BigInt& rightOperand) {
BigInt objToReturn("0");
// TODO: ADD LOGIC HERE
int carry = 0;
for (int i = 100; i > 0; i--){
int left = this->numArr[i] - 48;
int right = rightOperand.numArr[i] - 48;
int total = left + right;
if (total > 9){
carry = 1;
}else{
carry = 0;
}
total += carry;
total = total % 10;
objToReturn.numArr[i] = total + 48;
}
//num1 is the this object
cout << this->numArr[NUM_DIGITS];
//num2 is the rightOperand object
cout << rightOperand.numArr[NUM_DIGITS];
return objToReturn;
}
BigInt BigInt::operator+(const BigInt& rightOperand){
return add(rightOperand);
}
string BigInt::convertToString(){
// TODO: VALUE IN numArr CONVERTED TO STRING
int count = 0;
string str;
if(numArr[0] == 57){
tensComplement();
}
cout << "Output: " << str;
for (int i = 0; i < NUM_DIGITS; i++){
if(numArr[i] == 48 && count == 0){
}else{
str.push_back(numArr[i]);
count++;
}
}
cout << str << endl;
return str;
}
void BigInt::tensComplement(){
// TODO: TENS COMPLEMENT OF THIS NUMBER
for (int i = 0; i <= 100; i++) {
numArr[i] = 9 - numArr[i];
}
numArr[NUM_DIGITS] += 1;
for(int i = NUM_DIGITS; i >= 1; i--){
if(numArr[i] == 10){
numArr[i] = 0;
numArr[i - 1] += 1;
}
}
if(numArr[0] == 1){
numArr[0] = 9;
}
}
//This helps with testing.
bool checkTest(string testName, string whatItShouldBe, string whatItIs) {
if (whatItShouldBe == whatItIs) {
cout << "Passed " << testName << " last digit was: " << whatItIs.at(whatItIs.length()-1) << endl;
return true;
}
else {
if (whatItShouldBe == "") {
cout << "**Failed test " << testName << " ** " << endl << " Output was "<< whatItIs << endl << " Output should have been blank. " << endl;
} else {
cout << "**Failed test " << testName << " ** " << endl << " Output was "<< whatItIs << endl << " Output should have been " << whatItShouldBe << endl;
}
return false;
}
}
int main()
{
BigInt result;
BigInt num1("999");
BigInt num2("4873");
BigInt num3("-739");
BigInt num4("-9871");
// Test 1
checkTest("Test 1", "999", num1.convertToString());
// Test 2
checkTest("Test 2", "-739", num3.convertToString());
//Test some addition problems
// Test 3, negative + negative
result = num3.add(num4);
checkTest("Test 3", "-10610", result.convertToString());
return 0;
}