Hello again everyone, it's been awhile since I was here. I have another problem for anyone who is up for the challenge. I have a code here that I will post in a second, and what it is is a code that someone inputs 2 strings of numbers. The code then turns the strings of numbers into 2 integer arrays. It then adds the 2 arrays piece by piece and inputs that data into a new array. I am having problems with carrying a 1 to a new decimal place beyond the highest place inputed. Example being inputs are 900 and 100, it will display 000 and give me an error. Does anyone know how I could put this last decimal place in? I have tried for about 5 hours now and am finally turning to you all. Thanks again!
Code:
/*a program that allows someone to input a Very Long Integer(VLI), then adds and shows the sum and then compares
the two values */
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
void vliInput (int [], string, int&);
void vliOutput (const int [], const int);
void addVli (const int [], const int [], int[], const int, const int);
void compVli (const int [], const int[]);
const int MAXDIGITS = 100;
int main()
{
int vli1 [MAXDIGITS] = {0}, vli2[MAXDIGITS] = {0}, sum[MAXDIGITS] = {0}, size1 = 0, size2 = 0;
string num1, num2;
vliInput(vli1, num1, size1);//calls function to input first number
vliOutput(vli1, size1);//displays the number in integer form in an array
vliInput(vli2, num2, size2);//calls function to input second number
vliOutput(vli2, size2);//displays second number in integer form in an array
addVli(vli1, vli2, sum, size1, size2);//preforms additions
compVli(vli1, vli2);//compares the two arrays
return 0;
}
void vliInput (int vli[], string num, int& size)// input values for the string
{
cout << "Enter in a number between 1 and 100 digits: ";
getline(cin,num);//person inputs the number
size = num.length();//counts the amount of numbers inputed
for (int i = 0; i < size; i++)//turns the numbers into an array counting right to left
vli[MAXDIGITS - i - 1] = num[i] - 48;
}
void vliOutput (const int vli[], const int size)//output the number
{
for (int i = 0; i < size; i++)
cout << vli[MAXDIGITS - i -1];
cout << endl;
}
void addVli(const int vli1[], const int vli2[], int sum[], const int size1, const int size2)//adds the arrays
{
int size3 = 0;
int cr = 0;//integer used for a 1 carried
for (int i = 0; i < size1 || i < size2; i++)
{
sum[MAXDIGITS - 1 - i] = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])%10;//gets the sum
sum[MAXDIGITS - i + 1] += cr;//adds the carried one
cr = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])/10;//finds if there is a carried one
size3++;//counter for the size of the size for the total integer
}
if(cr == 1)//attempts to make the new space to add the 1, but has failed to do so
{
sum [MAXDIGITS - size3] += cr;
size3++;
}
for(int i = 0; i < size3; i++)//displays the number
cout << sum[MAXDIGITS - 1 - i];
cout << endl;
}
void compVli(const int vli1[], const int vli2[])//compares the two numbers
{
for(int i = 0; i < MAXDIGITS; i++)
{
if(vli1[i] > vli2[i])
{
cout << "Vli1 is larger than Vli2." << endl;
break;
}
else if (vli1[i] < vli2[i])
{
cout << "Vli2 is larger than Vli1." << endl;
break;
}
else if (vli1[i] == vli2[i] && i == MAXDIGITS - 1)
cout << "Vli1 and Vli2 are equal." << endl;
}
}