Sum of Arrays? Wrong output

Program: Take a 2 numbers that are 4 digits and perform an addition on it. The program should output the sum of the two numbers.

In my program I have made two strings (was required to use a string) that holds in a 4digit number. I find the length of the string which would be four. I'm going to go in a for loop and find the last digit of each numbers of the string use a subript. I will then add them. For instance 2345 and 2311. I will add 5 and 1. So this would keep repeating further on. My problem is that when I'm printing out the total sum I'm getting a
-8589934604656
Upon studying this code, I seem to figure out that the last 4 digits of this output is valid, but why am I getting these bunch of strange numbers in the beginning?
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void display (string &number1,string &number2)
{
	//Tell user to type in twenty digit
	cout<<"Twenty digit number: ";
	//get all the numbers until space is found
	getline(cin,number1);
			//If the length is greater then 20. ouput a message
				if (number1.length() >10)
					{
						number1.clear();
						cout<<"Enter Valid Number: ";
						cin>>number1;
					}
	cin.ignore(1000,'\n');
	
	//Tell user to enter in second twenty digit number
	cout<<"\n\nTwenty digit number: ";
	//get all the numbers until space is found
	getline(cin,number2);
			//If the length is greater then 20. ouput a message
				if (number2.length() >10)
					{
						number2.clear();
						cout<<"Enter Valid Number: ";
						cin>>number2;
					}
	cin.ignore(1000,'\n');
	
}



void addnumber_formula(string one,string two, int result[],int resultsize)
{
	//Declare sum to store the sum of indexes
	int sum=0;
	int lastnum=0;
	int carry=0;
	int row=0;
	int counter;

	//find the length of each string.
	int length1, length2;
	int max;
	length1=one.length();
	length2=two.length();
	//Determine which one of these is a max

	if (length1>length2)
		max=length1;
	else
		max=length2;

	//make a for loop to get each letter of a string
	for (counter=one.length()-1;counter>=0;counter--)
	{
		sum=(one[counter]-'0')+(two[counter]-'0')+carry;
		
		//Find the last number of sum
		lastnum=sum%10;

		//find first number of sum
		carry=sum/10;

		//store last number in the result
		result[row]=lastnum;
		row++;
		//if max is equal to one then make sure to enter carry in the array
			if (counter==1)
			{
				
				result[row]=carry;
			}
			
	}


}

void print_addition(int result[],int size)
{
	for (size=4;size>=0;size--)
		cout<<result[size];

}

int _tmain(int argc, _TCHAR* argv[])
{
	 
	//Cal he function to get the twenty digit number
	string one,two;
	display(one, two);
	int results[5];

	

	//Call addnumberformula
	addnumber_formula(one, two, results,5);
	print_addition(results,5);
	
Last edited on
By the way, in my pcode it says twenty digits. Please ignore that, I switched it to four digits.
Last edited on
You haven't initialised the array:
 
int results[5] = { 0 };


Actually, instead of an array of ints, you could use a std::string to store the result. Either concatenate each digit onto the front of the string. Something like this:
result = char ('0' + lastnum) + result;
or initialise the result string to be contain spaces, and be long enough to hold the answer, then use it as if it was an array.
Last edited on
Alright chervil after fixing that it did work. For general knowledge, can you explain to me why it did that, meaning outputting random numbers. In my add number void function, I'm storing a value in every array that has 5 elements, so where from where are these random numbers being outputted from?

I liked you other idea more better :D seems more easier :D
If you do not initialize a variable, calling that variable results in undefined behaviour. Most of the time it just takes a random (random enough for you at least) value from the stack.
I do understand that part. Feom what I see in my program, I have five elements of results. From my understanding all of those elements will have data stored in since that's the way I have made my function. There won't be a section or shouldn't be where no data is stored in the variable that will output a random number.
From my understanding all of those elements will have data stored in since that's the way I have made my function. There won't be a section or shouldn't be where no data is stored in the variable that will output a random number.

When the evidence belies your belief, you may want to change your belief.
Example: 2345 + 2311
result: 4656
Since the array is 5 digits but the result has only 4 digits, the leftmost digit retains its original, unchanged value. If the array is not initialised, the contents are not strictly speaking random, but they are "garbage", that is whatever value was already in that particular memory location from some previous use by another process.
Oh I get it now :D I even had a zero in the beginning which was confusing me but I figured out my counter wasnt working right. Thanks Chervil, redpandas and cire. Cire, I will work on improving that which I do seem to struggle with often at times :D
Topic archived. No new replies allowed.