Adding strings of integers

I'm trying to write an algorithm for a larger project that will take two strings which are both large integers (only using 10 digit numbers for the sake of this demo) and add them together to produce a final string that accurately represents the sum of the two original strings. I realize there are potentially better ways to have gone about this from the beginning but I am supposed to specifically use strings of large integers as opposed to a long integer.

My thinking was to take the two original strings, reverse them so their ones position, tens position, and so on all line up properly for adding. Then one position at a time, convert the characters from the strings to single integers and add them together and then use that sum as the ones position or otherwise for the final string, which once completed will also be reversed back to the correct order of characters.

Where I'm running into trouble I think is in preparing for the event in which the two integers from the corresponding positions in their strings add to a sum greater than 9, and I would then have carry over some remainder to the next position. For example, if I had 7 and 5 in my ones positions that would add to 12, so I would keep the 2 and add 1 to the tens position once it looped back around for the tens position operation.

I'm not getting results that are in any way accurate and after spending a large amount of time stumbling over myself trying to rectify my algorithm, I am not sure what I need to do to fix this.

Hopefully my intended process is clear and someone will be able to point me in the right direction or correct some mistake I may have in my program.

Thanks in advance.

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
#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;


int main ()
{
    string str1 = "1234567890", str2 = "2345678901"; //Two original strings of large integers
    string rev_str1, rev_str2;
    int int1 = 0, int2 = 0;
    string final; //Final product string, sum of two original strings
    int temp_int = 0, buffer_int, remainder = 0;
    string temp_str = "", buffer_str;
    char buffer[100] = {0};


    cout << "str1 = " << str1 << endl;
    cout << endl;
    cout << "str2 = " << str2 << endl;
    cout << endl;


    rev_str1 = string(str1.rbegin(), str1.rend());


    rev_str2 = string(str2.rbegin(), str2.rend());


    for (int i = 0; i < 10; i++)
    {
        buffer_str = rev_str1.at(i);


        int1 = atoi(buffer_str.c_str());


        buffer_str = rev_str2.at(i);


        int2 = atoi(buffer_str.c_str());


        buffer_int += (int1 + int2 + remainder);


        remainder = 0;


        while (buffer_int > 9)
        {
            buffer_int -= 10;


            remainder += 10;
        }


        temp_str = itoa(buffer_int, buffer, 10);


        final += temp_str;
    }


    final = string(final.rbegin(), final.rend());


    cout << "final = " << final << endl;


    cout << endl;
}



Tip: Pay attention to warnings when you compile.

main.cpp(46): warning C4700: uninitialized local variable 'buffer_int' used


Line 46 should be buffer_int = (int1+int2+remainder);

Better yet: int buffer_int = (int1+int2+remainder); as there is no reason for it to be defined elsewhere.
One way would be to hold each string in an array and use gets() in conjunction with atof() to convert each string to a double. Sum the two doubles.
for example:
1
2
3
4
5
6
7
double n,m;
char  s[81],s1[81];
cout<<"enter a string of digits  \n";
n =  atof(gets(s));
cout<<"enter a string of digits  \n";
m = atof(gets(s1));
cout<<"The sum of the two strings is: "<<n+m<<endl;

I'm not too sure if gets() is very highly recommended these days so I'll probably cop heaps.
std::string is (a) considerably safer, (b) considerably easier and (c) considerably harder to stuff up badly with, so it's definitely a better option. However, the atof - gets method would work.
Otherwise, atoi is a better function to use than atof (because the numbers are integers), although there is actually a function in <string>, stoi, which converts an std::string straight into an integer (stoui if you want an unsigned int).
both large integers (only using 10 digit numbers for the sake of this demo)

If the OP's compiler supports unsigned long long this would be the largest integer type available otherwise double or long double would have to be used.
Regardless of the type used, you would use a std::istringstream to convert the string into a number.
if the OP really wanted to, they could create a struct of indefinitely large size which has the +-*/% operators overloaded in the global scope. However, for the sake of any sane calculation, uint_64_t (<stdint.h>) would almost certainly be enough - after all, it can handle numbers greater than 10^20
Topic archived. No new replies allowed.