Hello so basically my project goes like this:
Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.
Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.
Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more
than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".
You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end.
And I'm having issues with my code. Can't seem to figure it out, what I have so far is:
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
const int MAXIMUM_DIGITS = 20;
int size; // number of digits in the integer
int digit[MAXIMUM_DIGITS]; // the digits stored in reverse order
void introduction();
void input_Large_Int(int a[], int& size_of_A);
int output_Large_Int(int a[], int size_of_A);
int output_Large_Int2(int b[], int size_of_B);
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);
int main()
{
int a[MAXIMUM_DIGITS], b[MAXIMUM_DIGITS], sum[MAXIMUM_DIGITS];
int size_of_A, size_of_B, size_Sum;
char ans;
for(int i = 0;i < MAXIMUM_DIGITS-1;i++)
{
a[i] =0;
b[i]=0;
sum[i]=0;
}
introduction();
do {
input_Large_Int(a, size_of_A);
input_Large_Int(b, size_of_B);
add(a,size_of_A,b,size_of_B,sum,size_Sum);
cout << "The sum of ";
output_Large_Int(a, size_of_A);
cout << " and ";
output_Large_Int(b, size_of_B);
cout << " is ";
cout << output_Large_Int(a, size_of_A) + output_Large_Int2(b, size_of_B) << endl << endl;
cout << endl << endl;
cout << "Would you like to add two more numbers? (y or n): ";
cin >> ans;
}
while (ans == 'y' || ans == 'Y');
system("PAUSE");
return 0;
}
void introduction()
{
cout << "This program will allow you to input two large integers and add\n"
<< "them together. Then you will be allowed to run the program again\n"
<< "and again depending on how many calculations you would like to make.\n"
<< endl << endl;
}
void input_Large_Int(int a[], int& size_of_A)
{
char digit[MAXIMUM_DIGITS];
char change;
int i = 0;
cout << "Please enter a positive integer with no more than " << MAXIMUM_DIGITS
<< " digits: ";
cin.get(change);
if (change == '\n')
cin.get(change);
while (isdigit(change) && i < MAXIMUM_DIGITS)
{
digit[i] = change;
i++;
cin.get(change);
}
size_of_A = i;
int j = 0;
while (i > 0)
{
i--;
a[j] = digit[i] - '0';
j++;
}
for (int i = j; i < MAXIMUM_DIGITS; i++)
a[i] = 0;
}
int output_Large_Int(int a[], int size_of_A)
{
for (int i = 0; i < size_of_A; i++)
cout << a[size_of_A - i - 1];
}
int output_Large_Int2(int b[], int size_of_B)
{
for (int i = 0; i < size_of_B; i++)
cout << b[size_of_B - i - 1];
}
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)
{
int i;
for(i = 0; i < MAXIMUM_DIGITS - 2; i++)
{
sum[i] += (a[i] + b[i]) % 10;
sum[i + 1] += (a[i] + b[i]) / 10;
}
i++;
sum[i] += (a[i] + b[i]) %10;
if ((a[i] + b[i])/10 > 0)
{
cout << "There was an overflow causing more than 20 integers.\n"
<< endl;
}
}