I've looked on the site and I've Google'd this for the past couple days to no avail. Either this is just completely obvious and I'm terrible at programming(I'm new, so this is possible).

Anyway, the assignment is to accept two whole numbers that are 512 digits or less. Then we have to subtract the two numbers. Obviously the number is too big for any numerical variable so they have to be stored in another type. I decided string would be the best option, but for some reason when I do my code I get either nothing or a bunch of garbage. I looked at the value of "ans" and the result was garbage. I'm pretty sure the problem is that it's outputting the results of the ASCII value instead of the int that I'm trying to place back into the string. The only libraries I'm allowed to use are the ones I've already included. I'm also supposed to add the two numbers, but if I can figure out how to do the subtraction I could do the addition. I'm not asking someone to write this program for me, just to point me in the right direction. Thanks.

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
char str1[512],str2[512],rev1[512],rev2[512],ans1[513],ans2[513];
int num1,num2,num3,num4,len1,len2,len3,len4,len5;
num1 = num2 = num3 = num4 = 0;
printf("Enter a whole number 512 digits or less:");
gets(str1);
printf("\nEnter another whole number 512 digits or less:");
gets(str2);
len1 = strlen(str1);
len2 = 0;
--len1;
while(len2 <= len1)//this loop reverses the number in the first string
{
rev1[len2] = str1[len1 - len2];
++len2;
}
rev1[len2] = '\0';
len2 = 0;
len1 = strlen(str2);
--len1;
while(len2 <= len1) //this loop reverses the number in the second string
{
rev2[len2] = str2[len1 - len2];
++len2;
}
rev2[len2] = '\0';

len3 = strlen(rev1);
len4 = strlen(rev2);
len1 = 0;
len2 = 0;
len5 = 0;
while((len1 <= len3)&&(len2<=len4))
{
num1 = rev1[len1];
num2 = rev2[len2];
num3 = num1 - num2;
if(num3 < 0)//This block is to "borrow" from the next number on the list if the result is less than 0
{
num4 = rev1[len1+1];
num4 = num4 - 1;
rev1[len1+1] = num4;
num3 = num3 + 10;
}
ans1[len5] = num3;
++len5;
++len1;
++len2;
}
len1 = strlen(ans1);
len2 = 0;
--len1;
while(len2 <= len1)//reversing the answer back to original order
{
ans2[len2] = ans1[len1 - len2];
++len2;
}
ans2[len2] = '\0';

printf("%s",ans2);
system("PAUSE");
return 0;
}``````

Could you please post a copy of the assignment here, so that we can understand what the exact challenge is?

You seem to not have a mechanism to change characters to integers. Try to use atoi(), which is in stdlib.h

1. Accept input of a whole number up to 512 digits
2. Accept input of a second whole number up to 512 digits with a value less than the first.
3. Display the sum of the two numbers.
4. Display the difference of the first number minus the second number.

You may use stdlib.h, stdio.h, string.h, math.h. No other libraries or external code may be used.

And the problem with atoi() is that it will continue until there is a NULL value. I'm currently working on copying one char from the string to another with just that char on it and then using atoi().

I just finished my attempt with atoi() and it didn't work.

``````while((len1 <= len3)&&(len2<=len4))
{
atoi1[0] = rev1[len1];
atoi2[0] = rev2[len2];
atoi1[1] = '\0';
atoi2[1] = '\0';
num1 = atoi(atoi1);
num2 = atoi(atoi2);
num3 = num1 - num2;
if(num3 < 0)//This block is to "borrow" from the next number on the list if the result is less than 0
{
atoi3[0] = rev1[len1+1];
num4 = atoi(atoi3);
num4 = num4 - 1;
rev1[len1+1] = num4;
num3 = num3 + 10;
}
ans1[len5] = num3;
++len5;
++len1;
++len2;
}``````

atoi1[0] = rev1[len1];
atoi2[0] = rev2[len2];
atoi1[1] = '\0';
atoi2[1] = '\0';
num1 = atoi(atoi1);
num2 = atoi(atoi2);

what are you trying to achieve here?
atoi just converts a string to an integer why put a null terminator at the end?

``````int i;
char Input [512];
printf ("Enter a number: ");
fgets ( Input, 512, stdin ); // or use a standard scanf
i = atoi (Input);
printf ("The value entered is %d",i);``````

what are you trying to achieve here?
atoi just converts a string to an integer why put a null terminator at the end?

``````int i;
char Input [512];
printf ("Enter a number: ");
fgets ( Input, 512, stdin ); // or use a standard scanf
i = atoi (Input);
printf ("The value entered is %d",i);``````

The problem with that is that that will continue to pull until there is a NULL value, and an int(or any numerical value, for that matter) can't hold a 512 digit number. I didn't have the null terminator originally and it gave me a bunch of garbage, but I got rid of it and just put the number in atoi[1] instead. And that still didn't work for me. And I have to input the answer back into a string in order to display it, and I can't get that to work, either.

Since your using a 512 digit number my suggestion would be to convert it to hexadecimal base or a higher base digit, do the operation then convert it back to string or decimal base

though I'm not sure if this is the best solution

...Maybe using a long int might help you more in storing the numbers

I actually got it to work...almost. I'm actually surprised with myself. I rewrote the code from scratch and it looks cleaner. The problem is that it runs until the end and then says "Run-Time Check Failure #2 - Stack around the variable 'toint1' was corrupted."

I'm using Visual Studio 2010.

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
int int1,int2,int3,int4,int5,len1,len2,len3,len4,len5;
char num1[512],num2[512],ans[512],toint1[1],toint2[1],toint3[1];
int1 = int2 = int3 = int4 = int5 = len5 = 0;
printf("Program: Advanced Repetition\nEnter a whole number:");
gets(num1);
printf("\nEnter a second whole number: ");
gets(num2);
len1 = strlen(num1);
num1[len1] = '\0';
len2 = strlen(num2);
num2[len2] = '\0';
len5 = strlen(num1);
len4 = len5;
while((len1 >= 0))
{
toint1[0] = num1[len1];
toint2[0] = num2[len2];
int1 = atoi(toint1);
int2 = atoi(toint2);
int3 = int1 - int2;
if(int3 < 0)
{
toint3[1] = num1[len1-1];
int4 = atoi(toint3);
int4 = int4 - 1;
int3 = int3 + 10;
}
itoa(int3,toint1,10);
ans[len5] = toint1[0];
--len5;
--len1;
--len2;
}
ans[len4] = '\0';
printf("Subtracting num1 from num2: %s\n",ans);
system("PAUSE");
return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.