Long integer addition, subtraction, multiplication problems!

Earlier today, my program was working alright, even though there were quite a few bugs left. Now, however, nothing seems to be working properly. It's difficult to explain, so I'll post some of the code I've been working on.

void char_to_int(char num1[33], char num2[33], int num1I[33], int num2I[33])
{
int n1, n2;
for (n1 = 0; num1[n1] != '\0'; n1++)
num1I[n1] = num1[n1] - '0';
alter_arr(num1I);
for (int i = 0; i < 5; i++)
cout << num1I[i];

for (n2 = 0; num2[n2] != '\0'; n2++)
num2I[n2] = num2[n2] - '0';
alter_arr(num2I);

//This section of code focuses on changing the characters in the array to
//integers. The alter_arr() function is to flip the arrays around so it
//adds properly. I'll post that function next:
}

void alter_arr(int arr[])
{
int i = 0;
int j = 49;
char temp[50] = { 0 };
while (arr[i] != NULL)
{
temp[j - getSize_int(arr)] = arr[i];
j++;
i++;
}

for (i = 0; i < 49; i++)
{
arr[i] = temp[i];
}
}
//The addition portion of it is:
int add(int carry, int sum[], int num1I[], int num2I[], int size1, int size2) //adds two numbers
{

int n1 = 0, n2 = 0;
int l = 49;
int k;
//why are zeros automatically being seen as NULL terminators? Or are they even being seen as that? Why don't they work?


n1 = size1 - 1;
//n2 = size2 + 1;
int i = 48;
int j = 48;

for (; i >= 0 && j >= 0; i--, j--, l--) //I got an idea from a friend to //just combine the for-loop into one huge loop. I don't like it.
{ //However, I'm using it for the experience.

sum[l] = (num1I[i] + num2I[j] + carry) % 10;
carry = (num1I[i] + num2I[j] + carry) / 10;

}
//reverses answer to correct direction

for (k = n1; k < 50; k++)
{
cout << sum[k];
}
cout << endl;


return 0;

}


//My main problem right now is my addition will not work if I have num1I or //num2I as anything with a zero in it...i.e. 1027 or 2001. What have I done //wrong? My brain hurts.
Why are zeros automatically being seen as NULL terminators? Or are they even being seen as that? Why don't they work?

No, it only applies to pointers.

Long integer addition, subtraction, multiplication problems!

Why does it have to be so difficult? If you want one, you can just use long long instead.
This is for a program in my CSC class. The professor was very specific how he wanted it done, and he wanted us to do everything the difficult way.
Why are zeros automatically being seen as NULL terminators?

Because NULL is zero. Null terminators apply to C character arrays.

In alter_arr(), you can't distinguish between a NULL and a legitimate value of 0 because they are the same value.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.