Search code examples
c++arraysdigit

Number addition program of up to 100 digits. Odd console output


The console outputs are my major concern. This is the entire program. I will post the console output at the very bottom. The outputs show what i input into the program. You can immediately see that the numbers are turned odd. They get negative numbers or maybe hyphens. The odd numbers are above and below the "+" signs near the bottom. I would like to add the two inputs and have them display correctly. Please provide whatever assistance you can. Even if it's to criticize my posting method as i am new.

#include <iostream>
#include <string>
using namespace std;

void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);

const int MAX_DIGITS = 100;

int main()
{
    int number1[MAX_DIGITS]={}, number2[MAX_DIGITS]={}, sum[MAX_DIGITS]={};
    bool finished = false;
    char response;
    while (! finished)
    {
        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number1);
        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number2);
        addBig(number1, number2, sum);
        printBig(number1);
        cout << "\n+\n";
        printBig(number2);
        cout << "\n=\n";
        printBig(sum);
        cout << "\n";
        cout << "test again?";
        cin >> response;
        cin.ignore(900,'\n');
        finished = toupper(response) != 'Y';
    }
    return 0;
}

void readBig(int number[MAX_DIGITS]) // This function below will read an input number //as a string then input that into an array.
{
    string read;
    cin >> read;

    for (int i = 0; i < MAX_DIGITS && i <= read.length() + 1; i++) {

        number[i] = int (read[i] - '0');
    }
}

// This function below will display the number.
void printBig(int number[MAX_DIGITS])
{
    int digit = 0; // first val counter

    for (int i=0; i<=MAX_DIGITS; i++) 

    if (number[i] == 0 && digit >= 1) cout << ""; // clears leading zeros 
    // and checks for placeholder zeros

    else cout << number[i]; digit++; // else print val and check 
    // for beginning of number.
}

void addBig(int number1[MAX_DIGITS], int number2[MAX_DIGITS], int sum[MAX_DIGITS])
    // The code below sums the arrays.
{
    for (int i = 0; i < MAX_DIGITS; i++) {
        sum[i] = number1[i] + number2[i];
    }
    for (int j = 0; j < MAX_DIGITS; j++){
        if (sum[j] / 10 > 0) { sum[j + 1] += 1; }
        if (sum[j] / 10 > 0) { sum[j] = sum[j] / 10; } // EDIT:I would like both to happen
    }
}

CONSOLE OUTPUT

    Please enter a number up to 100 digits: 987654321
    Please enter a number up to 100 digits: 987654321
    987654321-48-483
+
    987654321-48-489
=
    11111-9-99
    test again?

    Please enter a number up to 100 digits: 444444444
    Please enter a number up to 100 digits: 444666644
    444444444-48-483
+
    444666644-48-484
= 
    01111-9-94
    test again?

I would really like some help with figuring out the outputs, but any suggestions would be great :)!


Solution

  • Some specific problems:

    1) This code:

    else  cout << number[i]; digit++;
    

    What you meant was:

    else
    {
        cout << number[i];
        digit++;
    }
    

    What you wrote was:

    else
    {
        cout << number[i];
    }
    digit++;
    

    If you're new to C/C++, always use the curly braces, mostly to avoid this sort of problem but also to make your code clearer to others.

    2) Your representation is backwards. By putting the high order digits first in the number array, you leave no room for carry on the highest digits. However, if you reverse it and put the lowest order digits first, you can carry into the unused digits in the array.

    3) This declaration:

    int number1[MAX_DIGITS]={}, number2[MAX_DIGITS]={}, sum[MAX_DIGITS]={};
    

    Needs to initialize all the digits to zero for your algorithm (that doesn't track number length) to work. So it needs to be in the loop so the second addition doesn't inherit digits from the first.

    I've reworked your code with the above changes and many style changes:

    #include <iostream>
    #include <string>
    using namespace std;
    
    void readBig(int[]);
    void printBig(int[]);
    void addBig(int[], int[], int[]);
    
    const int MAX_DIGITS = 100;
    
    int main()
    {
        bool finished = false;     
    
        while (! finished)
        {
            int number1[MAX_DIGITS]={0}, number2[MAX_DIGITS]={0}, sum[MAX_DIGITS]={0};
            char response;
    
            cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
            readBig(number1);
            cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
            readBig(number2);
            addBig(number1, number2, sum);
            printBig(number1);
            cout << "\n+\n";
            printBig(number2);
            cout << "\n=\n";
            printBig(sum);
            cout << "\n";
            cout << "test again? ";
            cin >> response;
            cin.ignore(900, '\n');
            finished = toupper(response) != 'Y';
        }
    
        return 0;
    }
    
    void readBig(int number[MAX_DIGITS])
    {
        // This function below will read an input number as a string then input that into an array.
    
        string read;
        cin >> read; 
    
        int length = read.length(); 
    
        for (int i = length - 1, j = 0; i >= 0 && j < MAX_DIGITS - 1; i--)
        {
            number[i] = read[i] - '0';
        }
    }
    
    void printBig(int number[MAX_DIGITS])
    {
        // This function below will display the number.
    
        bool first_digit = false; // first value flag
    
        for (int i = MAX_DIGITS - 1; i >= 0; i--)
        {
            if (number[i] != 0 || first_digit)
            {
                // clears leading zeros and checks for placeholder zeros
                cout << number[i];
                first_digit = true; // else print val and check for beginning of number.
            }
        }
    }
    
    void addBig(int number1[MAX_DIGITS], int number2[MAX_DIGITS], int sum[MAX_DIGITS])
    {
        // The code below sums the arrays.
    
        for (int i = MAX_DIGITS - 1; i >= 0; i--)
        {
            sum[i] = number1[i] + number2[i];
    
            if (sum[i] > 9 && i < MAX_DIGITS - 1)
            { 
                sum[i + 1] += 1;
                sum[i] -= 10;
            }
        }
    }
    

    OUTPUT

    % ./a.out
    Please enter a number up to 100 digits: 444444444
    Please enter a number up to 100 digits: 444666644
    444444444
    +
    446666444
    =
    891110888
    test again? y
    Please enter a number up to 100 digits: 9999
    Please enter a number up to 100 digits: 9999
    9999
    +
    9999
    =
    19998
    test again? n
    %