I am writing a program for class that inputs two of chars values no larger than 20. Then it adds these to Values together and prints the sum. Say this is my input Value one 123 Value two 456.

This is how i input it into program for keyboard.
3
1
2
3
"123"3
4
5
6
"465"
"579"
I have two problems.
#1 If i have to values of different size it wont work. Like if Value one is 123 and Values two is 4567. Value one has a size of 3 value two 4. In this case i need to make it put value one into array of size 4 so it would be 0123+4567 so the 7 and 3 will line up.
how can i do this?

#2 I cant get it to carry a remainder. Say value one is 39 and Value two is 21. When i run the program it prints 510 where 9+1=10 and 3+2=5. It should print 60. How can i fix this.

int size1=0,size2=0,i;
char data1;
char data2;
int *pointer1;
int *pointer2;
int *pointer3;

cin>>size1;   //ENTER SIZE OF VALUE ONE

pointer1=new int[size1];
 for (i=0; i<size1; i++)
    {
      cin>>data1;     //ENTER VALUE ONE AS CHARS
       data1=data1-48;  //CONVERTS CHARS TO INTS
       pointer1[i]=data1; // FILLS ARRAY WITH VALUE ONE
    }
    
    for (i=0; i<size1; i++)
	{cout << pointer1[i];} //PRINTS VALUE ONE

    


cin>>size2;  //ENTER SIZE OF VALUE TWO


pointer2=new int[size2];
 for (i=0; i<size2; i++)
    {
      cin>>data2;  //ENTER VALUE TWO AS CHARS
       data2=data2-48; //CONVERTS CHARS TO INTS
       pointer2[i]=data2;  // FILLS ARRAY WITH VALUE TWO
    }
    
    for (i=0; i<size2; i++)
	{cout << pointer2[i];} //PRINTS VALUE TWO
    
	pointer3=new int[size2+1];
	for(i=0; i<size2+1; i++)
	{
		pointer3[i]=*pointer1+*pointer2; //ADDs VALUE ONE AND TWO BY COLUMN
	    ++pointer1;
	    ++pointer2;
	}
cout<<"  "<<endl;
 for (i=0; i<size2; i++)
	{cout << pointer3[i];}// PRINTS SUM
 return 0;
}

Recommended Answers

All 3 Replies

not sure if that is what you want. If all you want is growing array, you might need to check vector(STL)

#include <vector>
int main(){
    vector <int> myVector;
    for(int i=0; i<100; i++){
        myVector.push_back(i);
    //then you can add anything later
    myvector.push_back(100);
    }
}

Split your algorithm into pieces:
1. get the user input char by char
2. convert it into integer
(however it is much easier to get the whole number at once ("cin >>integer"), but I guess it is required in the task to grab the input by 1 char)
3. get the next number the same way
4. add them and print the result

Simplest way is to first convert the arrays to their corresponding integer values and then simple add them.

here is the code to convert array values to integer:-

#include <iostream>
#define MAX 20
using namespace std;

int convertToInt(int *, int );
int main()
{
    int num1[20] = {5,0,0,0};
    int num2[20] = {5,0,0};

    int int1 = convertToInt(num1,4);
    int int2 = convertToInt(num2,3);

    int sum = int1 + int2;
    cout<< endl << int1 << " + " << int2 << " = " << sum;

    return 0;

}

int convertToInt(int *array, int size) {
    int sum = 0;
    int place = 1;
    for(int i = 0; i < size; i++) {
        place = 1;
        for(int j = 0; j < size-i-1; j++) {
            place = place *10;
        }
        sum = sum + array[i] * place;
    }
    return sum;
}
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.