I'm doing a simple addition arithmetic using pointer array but i'm stuck at trying to print out my pointer array.

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';
    }

}

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;

   }     
}

However, i do not have the size for my pointer. So how do i print out my pointer array?

The size of each array should be the same as the size of the string that it got its values from.

size1 = s1.size();
size2 = s2.size();
intArray = new int[size1];
intArray2 = new int[size2];

Do you have to use arrays to create the integers? You could do it quite simply with math:

int value = 0;
for(int i = 0; i < size1; i++)
{
    if(s1[i] >= '0' && s1[i] <= '9')
    {
        value = (value * 10) + (s1[i] - '0');
    }
    else
    {
        cout << "invalid string\n";
        return
    }
}

value will contain the integer representation of the string.

One thing to keep in mind. When you're accepting user input, it would be wise to check that the input is correct. Assuming you're only accepting positive integers, any string with a character that isn't between '0' and '9' inclusive, should be invalid.

Edited 1 Month Ago by tinstaafl

Comments
Nice!
The article starter is a financial contributor. Sponsored articles offer a bounty for quality replies.