Adding 2 big Integers via C++ Array?

I’m trying to make a program that takes two big integers, puts them into a string array and then adds the values and reads prints it out in the correct order, but I’m having a fair amount of issues. For instance one of my tests 4321+765 is meant to return 5086 but says “Addition not possible” and I’m a bit confused as to how. It’s been awhile since I’ve done much algo stuff so I’m trying to get the hang of it better. Here is my code:

#include <string>
#include <iostream>
using std::string, std::cout, std::endl; //Include for debug outputs

#include "bigInt.h"



Any advice would be much appreciated! It seems I'm messing up somewhere with the logic for the carry but I can't figure out how...


char character(int x) {
    // Converts integer 0-9 to corresponding character '0-9'
    return '0' + x;
}

int number(char ch) {
    // Converts character '0-9' to corresponding integer 0-9 by subtracting ASCII value of 0
    if (ch >= '0' && ch <= '9') {
        return ch - '0';
    }
    return -1;
}

void reverseArray(int arr[], int n) {
    // Swap elements up to the middle of the array to reverse the order
    for (int i = 0; i < n / 2; ++i) {
        int temp = arr[i];
        arr[i] = arr[n - i - 1];
        arr[n - i - 1] = temp;
    }
}

bool add(const int A[], int nA, const int B[], int nB, int C[], int& nC) {
    // Check if there is enough space in the result array
    int maxLen = nA > nB ? nA : nB;
    if (maxLen + 5 > nC) {
        return false; // Insufficient space
    }

    // Initialize carry and index variables
    int carry = 0;
    int k = 0;
    for(int i=0;i<maxLen;i++){

        
    }
    // Perform addition digit by digit
    for (int i = 0; i < maxLen || carry > 0; ++i) {
        int digitA = i < nA ? A[i] : 0;
        int digitB = i < nB ? B[i] : 0;
        int sum = digitA + digitB + carry;
        carry = sum / 10; // Determine the carry for the next iteration
        C[k] = sum % 10; // Store the result digit
        k++;
    }


    // Update the length of the result array
    nC = k;
    reverseArray(C,nC);

    return true; // Addition successful
}


bool fromStringToArray(const string& str, int A[], int size, int& n) {
    n = str.length();
    cout<<"n: "<<endl;
    if (n > size || n == 0) {
        return false;
    }
    // Check for non-digit characters and store in reverse order
    for (int i = 0; i < n; i++) {
        if (!isdigit(str[i])) {
            return false;
        }
        A[n - 1 - i] = number(str[i]);
        cout<<"Testing index "<<n-1-i<<"has value "<<A[n-1-i]<<endl; // Store digits in reverse order
    }
    // Handle the case where the number is zero
    if (n == 1 && A[0] == 0) {
        n = 1; // Correct representation of zero
    }
    return true;
}

string addBigInt(const string& A, const string& B) {
    // Check if valid big integer and make placeholder arrays of size 100 to store the values
    int nA, nB, nC;
    int A_arr[100], B_arr[100];
    if (!fromStringToArray(A, A_arr, 100, nA) || !fromStringToArray(B, B_arr, 100, nB)) {
        // Call stringToArray and check for errors
        return "Error: Invalid big integer";
    }

     int C[100];
    if (!add(A_arr, nA, B_arr, nB, C, nC)) {
        // Call add function and check for errors
        return "Error: Addition overflow";
    }
    
    // Reverse the result array
    reverseArray(C, nC);
    
    string result;
    for (int i = 0; i < nC; ++i) {
        // Convert array back to string for formatting
        cout<<"Test C "<<character(C[i])<<" "<<endl;
        result += character(C[i]);
    }
    return result;
}

and my test :

#include <iostream>
using std::cout, std::cin, std::endl;

#include <string>
using std::string;

#include "bigInt.h"

int main() {
    const int size = 100;
    int A_arr[size];
    int B_arr[size];
    int C_arr[size];
    int nA, nB, nC;

    while (true) {
        string strA, strB;
        cout << "Enter the first big integer: ";
        cin >> strA;
        cout << "Enter the second big integer: ";
        cin >> strB;

        if (!fromStringToArray(strA, A_arr, size, nA) || !fromStringToArray(strB, B_arr, size, nB)) {
            // Calls fromStringtoArray and checks for valid input
            cout << "Invalid input. Please enter positive integers or zero." << endl;
            continue;  // Ask for input again
        }

        if (!add(A_arr, nA, B_arr, nB, C_arr, nC)) {
            // Call sum function and return error if addition not possible
            cout << "Error, addition not possible." << endl;
            continue;  // Ask for input again
        }

        // Output the formatted result
        cout << strA << endl;
        cout << "+" << endl;
        cout << strB << endl;
        cout << "---------" << endl;

        for (int i = 0; i < nC; ++i) {
            cout << C_arr[i];
        }
        cout << endl;

        // Ask user if they want to continue
        cout << "Do you want to add more numbers? (y/n): ";
        char cont;
        cin >> cont;
        if (cont != 'y' && cont != 'Y') {
            break;
        }
    }

    return 0;
}

There seems to be something I messed up with the logic because I get errors that addition isn’t possible for many number values that need a carry. This is odd to me since int C is 100 length so I’m not sure why it’s getting this error. The tests show it to have failed conversions in fromStringToArray with leading 0’s, multiple digits, and exact size limit. What do I need to change? We’re instructed not to use or or any other libraries for this one as well. Additionally, if I wanted this to be able to take bases from 10-16 and do conversions based on that, what would be the right path to start on?

New contributor

Zarina L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật