Adding two very large numbers

Hello there!

I am writing a program that adds two VERY large numbers. I have my algorithm set up and everything, except that when I compile the program, it breaks.

I am using visual studio 2013.

Can you guys help me please?

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
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
80
81
82
83
84
85
86
  #include <iostream>
using namespace std;

class Addition
{
private:
	int num1[255], num2[255], sum[255];
	int length1, length2;
	int carry, k, i = length1 - 1, j = length2 - 1;
	char string1[255], string2[255];
public:
	Addition(){};
	void ConvertCharToInt();
	void PrintSum();
};

void Addition::ConvertCharToInt()
{
	printf("Enter The First Number:");
	scanf_s("%s", &string1);
	printf("Enter The Second Number:");
	scanf_s("%s", &string2);

	//Converting char to integer

	for (length1 = 0; string1[length1] != '\0'; length1++)
	{
		num1[length1] = string1[length1] - '0';
	}

	for (length2 = 0; string2[length2] != '\0'; length2++)
	{
		num2[length2] = string2[length2] - '0';
	}

	for (; i >= 0 && j >= 0; i--, j--, k++)
	{
		sum[k] = (num1[i] + num2[j] + carry) % 10;
		carry = (num1[i] + num2[j] + carry) / 10;
	}

	if (length1 > length2)
	{

		while (i >= 0)
		{
			sum[k++] = (num1[i] + carry) % 10;
			carry = (num1[i--] + carry) / 10;
		}

	}
	else if (length1 < length2)
	{
		while (j >= 0)
		{
			sum[k++] = (num2[j] + carry) % 10;
			carry = (num2[j--] + carry) / 10;
		}
	}
	else 
	{
		if (carry > 0)
		{
			sum[k++] = carry;
		}
	}
}

void Addition::PrintSum()
{
	printf("Result:");
	for (k--; k >= 0; k--)
	{
		printf("%d", sum[k]);
	}
}

int main()
{
	Addition obj;
	obj.ConvertCharToInt();
	obj.PrintSum();

	system("PAUSE");
	return 0;
}
What do you mean by "it breaks"? Does it freeze? Does it cause a blue screen of death? Does it start corrupting your hard drive and then exit? You need to be specific.
I found out why it's breaking, something like: "Access violation...". Anyways, now it's working but its printing undesired output.

check this picture: http://i.imgur.com/6dIAuFL.png

it's printing a garbage number, the result should be 24.
Last edited on
You never assign a value to k.

A lot of your member variables should not be member variables at all and should instead be local variables inside the functions that use them.
I fixed it. Now I am having another issue:

when I enter two numbers
1
2
12
12


the result is 2, which must be 24.


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
80
81
82
83
#include <iostream>
#include <string>
using namespace std;

class Addition
{
private:
	int num1[255], num2[255], sum[255];
	char string1[255], string2[255];
public:
	Addition(){};
	void ConvertCharToInt();
};

void Addition::ConvertCharToInt()
{
	cout << "Enter the first number:";
	cin >> string1;
	cout << "Enter the second number:";
	cin >> string2; 

	int length1, length2;
	int carry = 0;
	int k = 0;
	int j = 0;
	int i = 0;

	/* convert character to integer*/

	for (length1 = 0; string1[length1] != '\0'; length1++)
	{
		num1[length1] = string1[length1] - '0';
	}
	for (length2 = 0; string2[length2] != '\0'; length2++)
	{
		num2[length2] = string2[length2] - '0';
	}

	for (; i >= 0 && j >= 0; i--, j--, k++) 
	{
		sum[k] = (num1[i] + num2[j] + carry) % 10;
		carry = (num1[i] + num2[j] + carry) / 10;
	}
	if (length1 > length2)
	{

		while (i >= 0)
		{
			sum[k++] = (num1[i] + carry) % 10;
			carry = (num1[i--] + carry) / 10;
		}

	}
	if (length1 < length2) 
	{
		while (j >= 0) 
		{
			sum[k++] = (num2[j] + carry) % 10;
			carry = (num2[j--] + carry) / 10;
		}
	}
	else 
	{
		if (carry > 0)
		{
			sum[k++] = carry;
		}
	}

	cout << "Result:";
	for (k--; k >= 0; k--)
	{
		cout << sum[k];
	}
}

int main()
{
	Addition obj;
	obj.ConvertCharToInt();
	system("PAUSE");
	return 0;
}
A little complex for my taste, but. . .to each his own :)

In line 39 - what are the values of i, j, k respectively when you enter the loop?

Try doing this:
1
2
3
4
5
6
7
	for (; i >= 0 && j >= 0; i--, j--, k++) 
	{
		cout << i << "\t" << j << "\t" << k << "\n";
		sum[k] = (num1[i] + num2[j] + carry) % 10;
		carry = (num1[i] + num2[j] + carry) / 10;
	}
cout << i << "\t" << j << "\t" << k << "\n";


in place of lines 39 through 43 and you'll see what I mean.
Topic archived. No new replies allowed.