Getting rubbish result when adding integer pointer array

I'm trying to do a simple arithmetic calculation with integer pointer array.

Firstly, the user will input a 2 string to add and reverse them.

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

int main()
{
	string str1, str2;
	char option;
	
	do {
		cout << "Enter the first string" << endl;
		cin >> str1;
		
		cout << "Enter the second string" << endl;
		cin >> str2;

		str1 = reverseString(str1);
		str2 = reverseString(str2);
	
		stringtoInt(str1, str2);
		
		cout << endl;
	
		cout << "Continue (y/n) : ";
		cin >> option;
		
		cout << endl;
	
	}while(option == 'y');
	
}

string reverseString(string s1)
{
	int i = 0;
	int j;
	
	char temp;
	
	j = s1.length() - 1;
	while (i < j)
	{
		temp = s1[i];
		s1[i] = s1[j];
		s1[j] = temp;
		
		i++;
		j--;
	}
	
	return s1;	 
	
}


In my main function, i then call to convert the string into an integer array pointer
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
void stringtoInt(string& s1, string& s2)
{
	int size1 = s1.size();
	int size2 = s2.size();
	
	intPtr intArray;
	intPtr intArray2;
	
	intArray = new int[size1];
	intArray2 = new int[size2];
	
	
	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);
					  
}


And after the conversion, i'll call to add both integer array pointer
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
void addInteger(intPtr intArray, intPtr intArray2)
{
	int *p = intArray;
	int *q = intArray2;
	
	intPtr total = new int[MAX];
	
	int i = 0;
	
	int sum = 0;
	int carried = 0;
	
	while (*p != '\0' || *q != '\0')
	{
		*(total + i) = *p + *q + carried;
		
		if(*(total + i) > 9)
		{
			*(total + i) %= 10;
			carried = 1;
		}
		
		else	
			carried = 0;	
			
		i++;
		p++;
		q++;			
	}	 
	
	return printArray(total, i);

	
}


However, when i attempted to print them out, it returns me a garbage of integer sums which i can't figure out what happened

1
2
3
4
5
6
void printArray(intPtr total, int size)
{	 
	for(int i = 0; i < size; i++)
		cout << *(total + i);
			
}


I've been trying to debug this for hours but i can't seem to figure out how. And i just started learning C++ not long ago.
Last edited on
while (*p != '\0' || *q != '\0')
This makes no sense. p and q are pointers to int arrays that you created. When did you put the value '\0' in them? Never.

You seem to have taken the idea that a C-string is an array of char with a '\0' on the end, and from this mistakenly inferred that any array of ints will also magically have a '\0' on the end. They won't.

Line 21 in your stringtoInt appears to be a cut and paste error. You're working on string one, twice, and nevre working on string 2.

I've been trying to debug this for hours but i can't seem to figure out how

Debugging is easy. It's asking yourself questions. here are the first questions to ask yourself?

1) Are the strings being read in correctly?
2) Are the strings being reversed correctly?
3) Are the reveresed strings being turned into int arrays correctly?

Here is how to answer the first two questions:

1)

1
2
3
4
5
6
7
                cout << "Enter the first string" << endl;
		cin >> str1;
                cout << "Check first string: " << str1 << endl;
		
		cout << "Enter the second string" << endl;
		cin >> str2;
                cout << "Check second string: " << str2 << endl;


2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void reverseString(string s1)
{

  cout << "About to reverse: " << s1 << endl;
   int i = 0;
   int j;
	
   char temp;
	
   j = s1.length();
   while (i < j)
    {
	temp = s1[i];
	s1[i] = s1[j];
	s1[j] = temp;
		
	i++;
	j--;
    }	
	
  cout << "Reversed string is: " << s1 << endl;
}



See how to start debugging? You just check the values of the the variables at points in the code and you see where they go wrong. It's very easy.



You were right. Somehow there's some issue when i reverse my string, but i'm still unable to print out the correct arithmetic calculation. Sorry, i'm still new to pointer. I can't really think of any other alternative way to do the calculation if the array are being declared as pointer.
Last edited on

Your function void reverseString(string s1) is being passed a copy of the string. It will work on that copy, and when the function finishes, the copy is thrown away. So even when you get the string reversing correctly, you will then be throwing away the reversed sting. You could return that copy, to make use of it, but you're not returning it; just discarding it.

You are using "pass by value". Now that you know the term for it, you can look up "pass by value" and "pass by reference" and understand what you're doing.


Array are for advanced users. As a beginner, you should be using std::vector.
Last edited on
One obvious problem with your reverseString function is that you're passing s1 by value, not reference. s1 goes out of scope when reverseString exits and the results are lost.
Sorry, i noticed it and i forgotten to change it accordingly.
Topic archived. No new replies allowed.