In this excersize you will write a program that reads in two positive intigers that are 20 or fewer digits in length and then outputs the sum of the two numbers. Your program will read in the digits as values of type char so that the number 1234 is read as the four characters '1' '2' '3' '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 revers the order of the elements in the array after the array is filled with data from the keyboard.

Your program will perform the addition but implementing the usual pap-and-pencil addition algorithm.The result is stored in an array of size 20, and the result is then written to the screen. If the result is more than 20 digits then your program should issue a message saying "Integer Overflow".

I understand what the final part is asking, but am I on the right track so far?

``````#include <iostream>

using namespace std;

// Sets the Maximum allowed number of digits to be entered in the array.
const int Max_Number = 20;

// Input function for the numbers
void input_Large_Int ( int a[], int& size_of_A );

// Outputs the sum of the 2 numbers
void output_Large_Int ( int a[], int size_of_A );

// The sum function for the program
void add ( int a[], int size_of_A, int sum[] );

int main()
{
//Declares the arrays
int a[Max_Number], b[Max_Number], sum[Max_Number];
int size_of_A,

char response='y';
while (response == 'y' || response == 'Y')
{

input_Large_Int( a, size_of_A );

}

cout << "Would you like to add two more numbers? ( Y-Yes, N-No ):" << endl;
cin >> response;
cout << endl;

}

void input_Large_Int ( int a[], int& size_of_A )
{
char num_1 [Max_Number], num_2 [Max_Number];

cout << "Please input the first number that has less than 20 numbers: " << endl;
cin >> num_1;
cout << endl;

cout << "Please input the second number that has less than 20 numbers: " << endl;
cin >> num_2;
cout << endl;
}``````

## All 3 Replies

So far, so good. You might want to format your code so we can read it easier next time you post.

This is just a little progress, I am trying to see if I am going about this properly. For some reason the if statements are inconsistent, as well as the addition portion. It is just a test algorithm to see if I did it properly and it appears not. Any advice as to how I should go about it?

``````#include <iostream>

using namespace std;

// Sets the Maximum allowed number of digits to be entered in the array.
const int Max_Number = 20;

// Input function for the numbers
void input( int num1[], int num2[] );

void total ( int num1[], int num2[] );

int main()
{
//Declares the arrays
int num1[Max_Number], num2[Max_Number], sum[Max_Number];
int size1, size2;

char response='y';

// Need a do while loop to repeat the program.
do
{
// Calls the Input Function
input( num1, num2 );

// Calls the sum function.
total ( num1, num2 );

// Asks for the user input to repeat the program.
cout << "Would you like to add two more numbers? ( Y-Yes, N-No ):" << endl;
cin >> response;
cout << endl;
}
// Allows the program to loop.
while (response == 'y' || response == 'Y');

}

// Functions!
void input ( int num1[], int num2[] )
{
// Declares the variables/chars we wish to input into the program.
char num_1 [Max_Number], num_2 [Max_Number];

cout << "Please input the first number that has less than 20 digits: " << endl;
cin >> num_1;
cout << endl;

// Checks to see if the number inputted is less than 20 digits.
if ( num_1[Max_Number] > 20 )
{
cout << "_________________________________________________________" << endl;
cout << "We cannot add this because it has more than 20 digits..." << endl;
cout << "(Please re-enter a number that is less than 20 digits): " << endl;
cin >> num_1; // Re-enter num_2 if the number is greater than 20 digits.
cout << endl;
}

cout << "Please input the second number that has less than 20 digits: " << endl;
cin >> num_2;
cout << endl;

// Checks to see if the number inputted is less than 20 digits.
if ( num_2[Max_Number] > 20 )
{
cout << "_________________________________________________________" << endl;
cout << "We cannot add this because it has more than 20 digits..." << endl;
cout << "(Please enter a number that is less than 20 digits): " << endl;
cin >> num_2; // Re-enter num_2 if the number is greater than 20 digits.
cout << endl;
}
}

void total ( int num1[], int num2[] )
{
// Sets the sum = 0 before adding.
int sum = 0;
// Redeclares the two varialbes.
char num_1, num_2;

// This is a test algorithm to see if the 2 numbers add correctly.
sum = num_1 + num_2;

cout << "We are going to calculate your large number..." << endl;
cout << "When you add " << num_1 << " and " << num_2 << " you will get " << sum << "." << endl;
}``````

This is just a little progress, I am trying to see if I am going about this properly....

2) When asking for help, details are very important. You gave us only vague ideas of what might be wrong.

The answer to the non question:

For some reason the if statements are inconsistent, as well as the addition portion.

is "make them consistent" since we don't understand your use of the term consistent nor what makes you think they are wrong.

3) And the other non-question:

It is just a test algorithm to see if I did it properly and it appears not.

Answer: Oh? What makes you think so?

We can only help if you explain what help you need. Vague statements do not help us understand your problems.

Be a part of the DaniWeb community

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