Making a simple addition calculator with a char array--how do I make the leading '0's blank?

Hi everyone, I'm in an introductory C++ course and we're learning how to add arrays together. For homework, I had to create a program that would take an input number with between 0-20 and store it in a size-20 char array. This was done two times.

I then had to take those two char arrays, convert each digit in the same slot to integers, add them together, and store the remaining int as a char in a third array (accounting for numbers that went over 10, as well).

Luckily I've already done all of that, but now I'm having a problem with the output (performed in the OutputNumber function). When storing the values in the arrays, we were told to fill all other empty array spots with char 0 values. When outputting, though, the number should not have any leading zeroes visible.

Here's an example of what an interaction with my current program would look like:

Please enter your first number
How many digits are in your number? 6
Please enter your number starting with the LEAST significant digit: 123456
Please enter your second number
How many digits are in your number? 7
Please enter your number starting with the LEAST significant digit: 1234567

00000000000000654321 + 00000000000007654321 = 00000000000008308642

The last line should just show "654321 + 7654321 = 8308642".
I've tried setting up a simple if statement to exclude zeroes, but obviously that excludes any zeroes that are actually contained in the number as well, which is a no no.

Any help will be greatly appreciated. Here's my code:

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
#include <iostream>
using namespace std;

const int SIZE=20;
char overflow;
void InitNumber(char[], char[], int, int);
void AddArrays(char[], char[], char[], char& overflow);
void OutputNumber(char[], char[], char[]);

int main() {
	int size, numDigits1, numDigits2;
	char digits1[SIZE];
	char digits2[SIZE];
	char sum[SIZE];
	char repeatAnswer;
	
	do{
		InitNumber(digits1, digits2, size, size);
		AddArrays(digits1, digits2, sum, overflow);
		if(overflow != 'y'){
			OutputNumber(digits1, digits2, sum);
		}
		
		cout << "\nWould you like to add two more numbers (y/n)? ";
		cin >> repeatAnswer;
	}
	while(repeatAnswer == 'y');
}

void InitNumber(char digits1[], char digits2[], int numDigits1, int numDigits2){
	int i;
	
	cout << "Please enter your first number\n" << "How many digits are in your number? ";
	cin >> numDigits1;
	
	//A loop continues until the user has input the total number of digits specified before
	cout << "Please enter your number starting with the LEAST significant digit: ";
	for(i = SIZE-1; i >= SIZE - numDigits1; i--) {
		cin >> digits1[i];
	}
	
	//Stores a 0 in each of the remaining spots of the array
	for(i = SIZE - numDigits1 - 1; i >= 0; i--) {
		digits1[i] = '0';
	}
	
	cout << "Please enter your second number\n" << "How many digits are in your number? ";
	cin >> numDigits2;
	
	//A loop continues until the user has input the total number of digits specified before
	cout << "Please enter your number starting with the LEAST significant digit: ";
	for(i = SIZE-1; i >= SIZE - numDigits2; i--) {
		cin >> digits2[i];
	}
	
	//Stores a 0 in each of the remaining spots of the array
	for(i = SIZE - numDigits2 - 1; i >= 0; i--) {
		digits2[i] = '0';
	}
}

void AddArrays(char digits1[], char digits2[], char sum[], char& overflow){
	int i, tempDigits1, tempDigits2, tempSum, tempOne = 0;
	
	for(i = SIZE-1; i >= 0; i--){
		tempDigits1 = digits1[i] - '0';
		tempDigits2 = digits2[i] - '0';
		tempSum = tempDigits1 + tempDigits2 + tempOne;
		if(tempSum >= 10){
			switch(tempSum){
				case 10:
					sum[i] = '0';
					tempOne = 1;
					break;
				case 11:
					sum[i] = '1';
					tempOne = 1;
					break;
				case 12:
					sum[i] = '2';
					tempOne = 1;
					break;
				case 13:
					sum[i] = '3';
					tempOne = 1;
					break;
				case 14:
					sum[i] = '4';
					tempOne = 1;
					break;
				case 15:
					sum[i] = '5';
					tempOne = 1;
					break;
				case 16:
					sum[i] = '6';
					tempOne = 1;
					break;
				case 17:
					sum[i] = '7';
					tempOne = 1;
					break;
				case 18:
					sum[i] = '8';
					tempOne = 1;
					break;
				case 19:
					sum[i] = '9';
					tempOne = 1;
					break;
				default:
					break;
			}
		}
		else{
			sum[i] = (tempDigits1 + tempDigits2 + tempOne) + '0';
			tempOne = 0;
		}
	}
	if((tempDigits1 + tempDigits2 + tempOne) >= 10){
		for(i = 0; i < SIZE; i++) {
			cout << digits1[i];
		}
		cout << " + ";
		for(i = 0; i < SIZE; i++) {
			cout << digits2[i];
		}
		cout << " = ERROR: Addition Overflow\n";
		overflow = 'y';
	}
}

void OutputNumber(char digits1[], char digits2[], char sum[]){
	int i, tempDigits1, tempDigits2;
	
	tempDigits1 = digits1[0] - '0';
	tempDigits2 = digits2[0] - '0';
	
	cout << endl;
	
	for(i = 0; i < SIZE; i++) {
			cout << digits1[i] - '0';
	}
	cout << " + ";
	for(i = 0; i < SIZE; i++) {
		cout << digits2[i];
	}
	cout << " = ";
	for(i = 0; i < SIZE; i++) {
		cout << sum[i];
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void print_number( const char digits[] ) {

    if( digits != nullptr ) { // sanity check

        for( ; *digits != 0 && *digits == '0' ; ++digits ) {;} // skip leading zeroes

        if( *digits == 0 ) std::cout << 0 ; // if the number is all zeroes, print one zero
        else for( ; *digits != 0 ; ++digits ) std::cout << *digits ; // otherwise, print remaining digits
    }
}

void OutputNumber( const char digits1[], const char digits2[], const char sum[] ) {

    std::cout << '\n' ;
    print_number(digits1) ;

    std::cout << " + ";
    print_number(digits2) ;
    
    std::cout << " = ";
    print_number(sum) ;
}

http://coliru.stacked-crooked.com/a/705311821d82057b
Topic archived. No new replies allowed.