Zip code program help

Good morning all, I am having a little trouble with getting the desired output from my program that converts numeric zip codes into their postal bar code equivalents. My code is below as well as my input along with current and desired outputs. Thank you in advance.

Input : 95003
Current output: ||:|:::|:|:||:::||:::::||:|
Desired output: ||:|:::|:|:||:::||:::::||:::||:|

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

// This program prompts users for a postal zip code and then converts these numeric values into their postal bar code equivalent.

string barCode(char digit)
{
    if (digit == '0')
        return  "||:::";
		
    else if (digit == '1')
        return ":::||";
		
    else if (digit == '2')
        return "::|:|";
		
    else if (digit == '3')
        return "::||:";
		
    else if (digit == '4')
        return ":|::|";
		
    else if (digit == '5')
        return ":|:|:";
		
    else if (digit == '6')
        return ":||::";
		
    else if (digit == '7')
        return "|:::|";
		
    else if (digit == '8')
        return "|::|:";
		
    else if (digit == '9')
        return "|:|::";
		
    else
        return "Invalid";

}

int main()
{
    string zipCode;
    string result;

    cout << "Please enter a zip code: ";
    cin >> zipCode;

    for ( unsigned int i = 0; i < zipCode.length(); i++ )
    {
        string currentBarcode = barCode (zipCode[i] );

        if ( currentBarcode.compare ("Invalid") == 0 )
        {
            cout << "You have entered a number that is not valid" << endl;
            break;
        }

        result = result + currentBarcode;
    }

    cout << "|" << result << "|";

    cout << endl;
    cin.ignore();
    return 0;
}
The current output looks correct for 95003 going by what you have in the if..else statements. The desired output seems to have an extra 3 at the end (950033).
Thank you for the reply wildblue. I forgot a valuable piece of information, the last digit is supposed to be a check digit, that when the sum of the zip code digits is computed, the check digit is supposed to be the closest number to this sum that creates a multiple of 10. In my example, 95003, 9+5+0+0+3 = 17, so the check digit would be 3. I would need my program to determine this check digit value and add the appropriate number to the output in bar code form. This has me stumped.
Ok so I have been working with a new code and am very close to having the program running, the only issue is that the check digit is being computed incorrectly. I know this code is a mess and could be cleaned up a bunch but I just need this check digit to output correctly, everything else works perfectly.

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
177
178
179
180
181
182
183
int error;
char a [] = ":::||";
char b [] = "::|:|";
char c [] = "::||:";
char d [] = ":|::|";
char e [] = ":|:|:";
char f [] = ":||::";
char g [] = "|:::|";
char h [] = "|::|:";
char i [] = "|:|::";
char j [] = "||:::";
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
int digit4 = 0;
int digit5 = 0;
int digit6 = 0;
int temp = 0;
int temp1 = 0;

int main()
{
	\
	std::cout << "Enter the zipcode! \n";
	std::cin >> temp;
	
	//The initial if statement determines if the number entered has more than 5 digits and decides which procedure to use
	if (temp / 10000 > 9){
		temp1 = temp;

	//Set each digit to a seperate variable
	if (temp % 10 > 0){
		do{ 
		(temp1 = temp1 / 10);
		}
		while (temp1 / 10 > 0);
	}
	digit1 = temp1;

		temp1 = temp;
	if (temp % 10 > 0){
		do{ 
		(temp1 = temp1 / 10);
		}
		while (temp1 / 10 > 10);
	}
	digit2 = temp1 - (digit1 * 10);

			temp1 = temp;
	if (temp % 10 > 0){
		do{ 
		(temp1 = temp1 / 10);
		}
		while (temp1 / 10 > 100);
	}
	digit3 = temp1 - ((digit1 * 100)+(digit2*10));

		temp1 = temp;
	if (temp % 10 > 0){
		do{ 
		(temp1 = temp1 / 10);
		}
		while (temp1 / 10 > 1000);
	}
	digit4 = temp1 - ((digit1 * 1000)+(digit2*100)+(digit3*10));

		temp1 = temp;
	if (temp % 10 > 0){
		do{ 
		(temp1 = temp1 / 10);
		}
		while (temp1 / 10 > 10000);
	}
	digit5 = temp1 - ((digit1*10000)+(digit2*1000)+(digit3*100)+(digit4*10));


	}else {
	digit5 = temp % 10;
	temp = temp / 10;
	digit4 = temp % 10;
	temp = temp / 10;
	digit3 = temp % 10;
	temp = temp / 10;
	digit2 = temp % 10;
	temp = temp / 10;
	digit1 = temp % 10;}


	//this portion checks to see if there are more than five digits and puts a boolean value into error accordingly
	if ((temp / 10) > 0) error = 1;
	else error = 0;
	std::cout << "If the following value is the same as the first five digits of the value entered, the digit distribution worked properly.\n";
	std::cout << digit1 << digit2 << digit3 << digit4 << digit5 << "\n";
	
	//COnvert digits to barcodes
	std::cout << "|";

	if (digit1 == 1) std::cout << a;
	else if (digit1 == 2) std::cout << b;
	else if (digit1 == 3) std::cout << c;
	else if (digit1 == 4) std::cout << d;
	else if (digit1 == 5) std::cout << e;
	else if (digit1 == 6) std::cout << f;
	else if (digit1 == 7) std::cout << g;
	else if (digit1 == 8) std::cout << h;
	else if (digit1 == 9) std::cout << i;
	else if (digit1 == 0) std::cout << j;

	if (digit2 == 1) std::cout << a;
	else if (digit2 == 2) std::cout << b;
	else if (digit2 == 3) std::cout << c;
	else if (digit2 == 4) std::cout << d;
	else if (digit2 == 5) std::cout << e;
	else if (digit2 == 6) std::cout << f;
	else if (digit2 == 7) std::cout << g;
	else if (digit2 == 8) std::cout << h;
	else if (digit2 == 9) std::cout << i;
	else if (digit2 == 0) std::cout << j;

	if (digit3 == 1) std::cout << a;
	else if (digit3 == 2) std::cout << b;
	else if (digit3 == 3) std::cout << c;
	else if (digit3 == 4) std::cout << d;
	else if (digit3 == 5) std::cout << e;
	else if (digit3 == 6) std::cout << f;
	else if (digit3 == 7) std::cout << g;
	else if (digit3 == 8) std::cout << h;
	else if (digit3 == 9) std::cout << i;
	else if (digit3 == 0) std::cout << j;

	if (digit4 == 1) std::cout << a;
	else if (digit4 == 2) std::cout << b;
	else if (digit4 == 3) std::cout << c;
	else if (digit4 == 4) std::cout << d;
	else if (digit4 == 5) std::cout << e;
	else if (digit4 == 6) std::cout << f;
	else if (digit4 == 7) std::cout << g;
	else if (digit4 == 8) std::cout << h;
	else if (digit4 == 9) std::cout << i;
	else if (digit4 == 0) std::cout << j;

	if (digit5 == 1) std::cout << a;
	else if (digit5 == 2) std::cout << b;
	else if (digit5 == 3) std::cout << c;
	else if (digit5 == 4) std::cout << d;
	else if (digit5 == 5) std::cout << e;
	else if (digit5 == 6) std::cout << f;
	else if (digit5 == 7) std::cout << g;
	else if (digit5 == 8) std::cout << h;
	else if (digit5 == 9) std::cout << i;
	else if (digit5 == 0) std::cout << j;

	//Check digit calculation

	if (error == 0){
	int temporary = digit1 + digit2 + digit3 + digit4 + digit5;
	int temporary1 = temporary;
	if (temporary % 10 != 1){
	do {
		temporary1++;
	}while (temporary1 % 10 != 1);
	}
	digit6 = temporary1 - temporary;
	
	if (digit6 == 1) std::cout << a;
	else if (digit6 == 2) std::cout << b;
	else if (digit6 == 3) std::cout << c;
	else if (digit6 == 4) std::cout << d;
	else if (digit6 == 5) std::cout << e;
	else if (digit6 == 6) std::cout << f;
	else if (digit6 == 7) std::cout << g;
	else if (digit6 == 8) std::cout << h;
	else if (digit6 == 9) std::cout << i;
	else if (digit6 == 0) std::cout << j;
	}
	else
		std::cout << "error - you entered more than 5 digits \n";
	
	//lastly, the closing | for the barcode
	std::cout << "| \n";
	
	return 0;
	}
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
#include <iostream>
#include <string>

int char_to_int( char c ) // convert char to int ( '7' to 7 etc. )
{
    // '1' - '0' == 1,    '4' - '0' == 4,   '8' - '0' == 8 etc.
    if( std::isdigit(c) ) return c - '0' ;
    else return 10 ; // error, invalid
}

int check_digit( std::string str )
{
    int sum = 0 ;

    // sum up the numeric value of all the digits
    for( unsigned int i = 0 ; i < str.size() ; ++i ) sum += char_to_int( str[i] ) ;

    const int remainder = sum%10 ; // remainder when divided by 10

    if( remainder == 0 ) return 0 ; // already a multiple of 10
    else return 10 - remainder ; // closest number to this sum that creates a multiple of 10.
}

const std::string invalid = "Invalid" ; // avoid hard-coded literals used in more than one place

std::string barCode( char digit )
{
    // we can use a lookup table to get the bar code
    static const std::string codes[] =
    {
      "||:::", // 0
      ":::||", // 1
      "::|:|", // 2
      "::||:", // 3
      ":|::|", // 4
      ":|:|:", // 5
      ":||::", // 6
      "|:::|", // 7
      "|::|:", // 8
      "|:|::", // 9
      invalid // invalid
    };

    // convert char digit to its int value, and lookup in the table
    return codes[ char_to_int(digit) ] ;
}

int main()
{
    std::string zipCode;
    std::string result;

    std::cout << "Please enter a zip code: ";
    std::cin >> zipCode;

    for ( unsigned int i = 0; i < zipCode.length(); i++ )
    {
        const std::string currentBarcode = barCode ( zipCode[i] );

        if ( /*currentBarcode.compare ("Invalid") == 0*/ currentBarcode == invalid )
        {
            std::cout << "You have entered a number that is not valid\n" ; // << endl;
            return 1 ; // break;
        }

        result += currentBarcode ;
    }

    // add check digit as the last digit
    char last_digit = check_digit(zipCode) + '0' ; // convert to a char: 3 + '0' == '3' etc
    result = result + barCode(last_digit);

    std::cout << "|" << result << "|\n";
}

http://coliru.stacked-crooked.com/a/35a33abcd0225f3e
Last edited on
Topic archived. No new replies allowed.