Addition carry problem

I simply don't know what else to do to get the number to carry over and open a spot in the array so it can show up. I've got the coding for the rest of the program figured but this. The addition function starts on line 129.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176


#include<iostream>
#include<cmath>

using namespace std;

const int ARRAY_SIZE = 20;//Constant for Array_Size
const int ARRAY_SIZE_2 = 20;//Constant for Array_Size_2
const int ASCII = 48;//Constant for the ASCII character 0
const int ASCII2 = 58;//Constant for the ASCII character 9
void getInput(char input1[]);//Function declaration for getInput
void getInput2(char input2[]);//Function declaration for getInput
void fillZero(int conInput1[], int conInput2[], int newOutput[]);//Function declaration for fillZeros
void conversion(char input1[], char input2[],int conInput1[], int conInput2[]);//Function declaration for the conversion
void addition(int conInput1[], int conInput2[], int newOutput[]);//Function declaration for the addition
void printResults(int conInput1[], int conInput2[], int newOutput[]);//Function declaration for output
char repeat();//Function declaration to repeat program

int main()
{    //Variable used in main 
     char ans;
     char input1[ARRAY_SIZE];
     char input2[ARRAY_SIZE];
     int conInput1[ARRAY_SIZE];
     int conInput2[ARRAY_SIZE];
     int newOutput[ARRAY_SIZE_2];


     do
     {
          fillZero(conInput1, conInput2, newOutput);//Calls fillZero function
          getInput(input1);//Calls getInput function
          getInput2(input2);//Calls getInput2 function
          conversion(input1, input2, conInput1, conInput2);//Calls the conversion function
          addition(conInput1, conInput2, newOutput);//Calls the addition function
          printResults(conInput1, conInput2, newOutput);//Calls the printResults function
          ans = repeat();//Calls the repeat function
     } while (ans == 'y' );
     return 0;
}

void fillZero(int conInput1[], int conInput2[], int newOutput[])//Fills the three Arrays used with zeros.
//Making sure there are no junk numbers in the memory.
{
     for ( int i = 0; i < ARRAY_SIZE; i++ )//For loop used to count up the numbers to the maximum number
     {
          conInput1[i] = 0;//Sets conInput1 to 0
          conInput2[i] = 0;//Sets conInput2 to 0
          //Tests used to check arrays are 0's
          //cout << conInput1[i];
          //cout << endl;
          //cout << conInput2[i];
          //cout << endl;
          //cout << newOutput[k];
     }
     for ( int k = 0; k < ARRAY_SIZE_2; k++ )//For loop used to count up the numbers to the maximum number
     {
          newOutput[k] = 0;//Sets newOutput to 0
     }
}

void getInput(char input1[])//Asks the user to input numbers for the program. 20 numbers will be used.
{
     int count=0;//Sets variable count to 0
     char c;
     //Output used to instruct user to input data
     cout << "\nPlease enter the first non-negative " << ARRAY_SIZE << " or less digit integer. "
          << "(Please enter one digit at a time followed by the return key. " <<  endl;
          
     for( int i = 0; i < ARRAY_SIZE; i++ )//For loop used to count up the numbers to the maximum number
     input1[i] = '\0';//Sets input1[i] to an empty string

     while (c != '.' && count < ARRAY_SIZE)//Check used to make sure no decimals are entered and the # of numbers is less than 20
     {
          cin >> c;//Reads the data into the array from the user
          if(c >= ASCII && c < ASCII2)//Check to make sure the numbers entered are correct
          {
               input1[count] = c;//Fills the input1 array
               count++;//Iterates the numbers up to the end of the array

          }
     input1[count]='\0';//Used to terminate the string

     }
}

void getInput2(char input2[])//Asks the user to input numbers for the program. 20 numbers will be used.
{
     int count=0;//Sets variable count to 0
     char d;
     //Output used to instruct user to input data

     cout << "\nPlease enter the first non-negative " << ARRAY_SIZE << " or less digit integer. "
          << "(Please enter one digit at a time followed by the return key. " <<  endl;
          
     for( int j = 0; j < ARRAY_SIZE; j++ )//For loop used to count up the numbers to the maximum number
     input2[j] = '\0';//Sets input2[j] to an empty string

     while (d != '.' && count < ARRAY_SIZE)//Check used to make sure no decimals are entered and the # of numbers is less than 20
     {
          cin >> d;//Reads the data into the array from the user
          if(d >= ASCII && d < ASCII2)//Check to make sure the numbers entered are correct
          {
               input2[count] = d;//Fills the input1 array
               count++;//Iterates the numbers up to the end of the array

          }
     input2[count]='\0';//Used to terminate the string
     
}
}

void conversion(char input1[], char input2[], int conInput1[], int conInput2[])//Function definition for conversion
{
     cout << endl<< endl;     

     for ( int i = 0; i < ARRAY_SIZE; i++ )//For loop used to count up the numbers to the maximum number
     {
          conInput1[i] = (input1[i] - ASCII);//Converts the char(input1[i]) array into int conInput1[i] array by 
          //subtracting the ASCII value of 48 from the digits.
          //cout << conInput1[i];//Test for the conversion
          conInput2[i] = (input2[i] - ASCII);//Converts the char(input1[i]) array into int conInput2[i] array by 
          //subtracting the ASCII value of 48 from the digits.
          //cout << conInput2[i];//Test for the conversion
     }
}

void addition(int conInput1[], int conInput2[], int newOutput[])//Addition function declaration
{    
     //Local variables
     int x = 0;
     int val = 0;
     
     for ( int i = 0; i < ARRAY_SIZE_2; i++ )//For loop used to count up the numbers to the maximum number
     {
          x = conInput1[i] + conInput2[i];//Adds the conInput1 and conInput2 funtions together for a total sum. 
          val = x / 10;//val is equal to the total of two digits in the array added together and then divided by ten
          //It converts the number into a double type but is not a doulbe therefor the portion after the decimal drops of leaving a whole number.
          //cout << val << " val" << endl;//Test for val
          newOutput[i]= x % 10;//newOutput[i] is being set to the total x being modulus divided by 10 to leave the remainder in the total

          newOutput[i-1] = newOutput[i-1] + val;//Adding the carry over number to the next column.
     }
}

void printResults(int conInput1[], int conInput2[], int newOutput[])//Prints out the results from the calculations
{
     cout << endl << endl;
     for ( int i = 0; i < ARRAY_SIZE; i++ )//For loop used to count up the numbers to the maximum number
     {
          cout << conInput1[i];//Test output
     }
     cout << endl;
     for ( int j = 0; j < ARRAY_SIZE; j++ )//For loop used to count up the numbers to the maximum number
     {
          cout << conInput2[j];//Test output
     }
     cout << endl << "--------------------" << endl;//Draws a line used as a seperator bar
     for ( int k = 0; k < ARRAY_SIZE_2; k++ )//For loop used to count up the numbers to the maximum number
     {
          cout << newOutput[k];//Output for the total.
     }       
}

char repeat()//Repeat function declaration
{
     char ans;
     cout << endl;     
     //Asks the user if he/she would like to repeat the program
     cout << "\nWould you like to repeat the program?"
          << "\nPress y for yes.";
     cin  >> ans;//Input from the user
     return ans;   
}
Last edited on
A description of the program would be helpful.
You seem to be adding your arrays from the zero element to the top. That suggests that your zero element is your least significant digit. In which case shouldn't you carry in the other direction?
 
newOutput[i + 1] = newOutput[i + 1] + val;

Also you overwrite your carried over digit when you do your addition:
 
newOutput[i]= newOutput[i] + (x % 10); // did you mean this so it adds to the carry? 

Obviously your newOutput array must be set to zero first
Topic archived. No new replies allowed.