0

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?

2
Contributors
1
Reply
19
Views
8 Months
Discussion Span
Last Post by tinstaafl
1

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 by tinstaafl

Votes + Comments
Nice!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.