Carry Addition Problem in BigIntegers uptp 45 digits in c++

I want to take a carry if number is greater than 99999999 the digit at every index of array must be less than or equal to 99999999 Here is my code

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
 
# include <string>
#include <iostream>
using namespace std;
# include "BigInteger.h"
void BigInteger::getNumber(string b)
{
	int start = 0;
	string subString[5];
	int zain = 1;
	bigInteger = b;
	int length = bigInteger.length();
	int counter = 44 - length;
	while (counter >= 0)
	{
		bigInteger = "0" + bigInteger;
		counter--;
		zain++;
	}
	for (int i = 0; i < 5; i++)
	{
		subString[i] = bigInteger.substr(start, 9);
		start = start + 9;
	}

	for (int i = 0; i < 5; i++)
	{
		bigIntegerArray[i] = stoi(subString[i]);
	}
}


void BigInteger::print()
{
	cout << bigInteger;
}

BigInteger BigInteger :: operator + (BigInteger obj)
{
	BigInteger temp;
	int n;
	for (int i = 4; i >=0; i--)
	{
		n = bigIntegerArray[i] + obj.bigIntegerArray[i];
		if (n > 999999999)
		{

		}
	}
}

I think we need more information. For example, in the code you posted, bigIntegerArray and bigInteger aren't defined. Looking at your + operator, you probably want to do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BigInteger BigInteger :: operator + (BigInteger obj)
{
	BigInteger temp;
	int n;
	int carry = 0;
	for (int i = 4; i >=0; i--)
	{
		n = bigIntegerArray[i] + obj.bigIntegerArray[i] + carry;
		if (n > 999999999) {
			carry = 1;
			n -= 100000000;
		}
		temp[i] = n;
	}
	if (carry) {
		// You overflowed.  Do Something.
	}
	return temp;
}
Topic archived. No new replies allowed.