Printing values from a pointer array

Here i have is a arithmetic addition calculator that has no value boundaries.
Firstly, i used a string to read in 2 values, then i store them into an integer array before adding them and adding to another total array. All array are connected via pointer. However, i got a problem trying to print them out.

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
  int main()
{
	string str1, str2;
	
	cout << "Enter the first string" << endl;
	cin >> str1;
	
	cout << "Enter the second string" << endl;
	cin >> str2;
	
	stringtoInt(str1, str2);
}

void stringtoInt(string& s1, string& s2)
{
	int size1, size2;
	int i = 0;

	
	intPtr intArray;
	intPtr intArray2;
	
	intArray = new int[MAX];
	intArray2 = new int[MAX];
	
	size1 = s1.size();
	size2 = s2.size();
	
	for (int i = 0; i < size1; i++)
	{
		intArray[i] = s1[i] - '0';
	}
	
	
	for (int i = 0; i < size2; i++)
	{
		intArray2[i] = s1[i] - '0';
        }
	
   addInteger(intArray, intArray2);		  
}

void addInteger(intPtr intArray, intPtr intArray2)
{
int *p = &intArray[0];
int *q = &intArray2[0];
	
intPtr total;
	
total = new int[MAX];
	
int i = 0;
	
int sum = 0;
int carried = 0;
	
while (*p >= 0 || *q >= 0 || carried == 1)
{
	total[i] = *p + *q + carried;
		
	if(total[i] > 9)
	{
		total[i] %= 10;
		carried = 1;
	}
		
	else
	{	
		carried = 0;
	}
		
  }
	
}


Usually when printing an array i require a size, but my design does not have a size. What could be an alternative way to print the total array.
1
2
3
4
5
	intPtr intArray;
	intPtr intArray2;
	
	intArray = new int[MAX];
	intArray2 = new int[MAX];

Why not this?
 
intArray = new int[s1.size( )];


1
2
3
4
5
6
7
	size1 = s1.size();
	size2 = s2.size();

        for (int i = 0; i < size1; i++)
	{
		intArray[i] = s1[i] - '0';
	}

Again, why not
 
for (int i = 0; i < s1.size( ); i++)


int i = 0; declared on line 17 is unused.

1
2
int *p = &intArray[0];
int *q = &intArray2[0];

is equivalent to this
 
int *p = intArray;


Try to be consistent. If you used intPtr in the same context throughout your program, why isn't it used here?

1
2
3
intPtr total;
	
total = new int[MAX];

can be shortened to
 
intPtr total = new int[MAX];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (*p >= 0 || *q >= 0 || carried == 1)
{
	total[i] = *p + *q + carried;
		
	if(total[i] > 9)
	{
		total[i] %= 10;
		carried = 1;
	}
		
	else
	{	
		carried = 0;
	}
		
  }
	
}

You're not progressing your pointers and i, so you have an infinite loop.
Also, you need to start from the last element of the array.
Last edited on
Topic archived. No new replies allowed.