Postal Barcode Lab Issues

Hi!! so for my CS lab were supposed to encode a 5 digit zip code into bars (using : and |'s) then we have to have a check digit where were supposed to add up all the digits and chose a check digit that is closest to make the sum a multiple of 10. so as an example were given that the zip code is 95014 and it has a sum of 19 so our check digit would be 1 since 19 + 1 = 20. and then basically that check digit is added to the end of the barcode. So pretty much the barcode for 95014 should look like |:|::(9) :|:|:(5) ||:::(0) :::||(1) :|::|(4) :::||(1). I just put the number in there to show the bars for that number. You dont need the numbers in the code like i have shown there. So i have most of the code but im having "strays"(?) and it wont read my code. Please help me out. I think i have most of it but I just really need some new eyes looking at it and telling me what im doing wrong. I would greatly appreciate it. Thank you so much!!

so this is 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>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


string int2Code(int i)
{
string result = "";
switch(i) // switching between accepted digits depending on the given parameter
{
case 1:
result = ":::||";
break;
case 2:
result = "::|:|";
break;
case 3:
result = "::||:";
break;
case 4:
result = ":|::|";
break;
case 5:
result = ":|:|:";
break;
case 6:
result = ":||::";
break;
case 7:
result = "|:::|";
break;
case 8:
result = "|::|:";
break;
case 9:
result = "|:|::";
break;
case 0:
result = "||:::";
break;
default: // in case the argument didn’t fall in any of the above cases
result = ""; // means failure
break;
}

// returning the barcode of the given int parameter
return result;
}



string checkDigit(int zipCode)
{
int result = 0;
int base = zipCode;

int code = 5;
while(code > 0)
{

	result += base%10;
	base = base/10;
	code–-;
}

result = 10-(result%10);

return int2Code(result); //returns empty string if the conversion fails
}


string encodePrefix(int zipCode)
{

	string result = "";

	int base = zipCode;

	int code = 5;

	while(code > 0)
	{

		result = int2Code(base%10) + " " + result;
		base = base/10;
		code–-;
	}

	return result;
}

string encode(int zipCode)
{

	string prefix = "|" + encodePrefix(zipCode);

	string sufix = checkDigit(zipCode)+"|";

// returns the zip code using the postnet barcode representation
	return (prefix + sufix);
}


template <class T>
bool fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
{
	std::istringstream iss(s);
	return !(iss >> f >> t).fail();
}

int main(int argc, char *argv[])
{
	string str;
	int zipCode;

	startBarcodeConverter:


	if(argc == 2)
		str = argv[1];
	else
	{

		cout << "Enter zip code : ";
		cin >> str;
	}


	if(fromString<int>(zipCode, std::string(str), std::dec) && str.length()==5)
	{
		string code = encode(zipCode);
		cout << "This is your barcode-> " << code << endl;
		cout << "————————————————–" << endl;


		string answer;
		cout << "\nConvert another zip code? (y/n) : ";
		cin >> answer;
		if(answer == "yes" || answer == "y")
			goto startBarcodeConverter;
	}
	else
	{
		cout << "Your zip code is not formatted correctly" << endl;
		goto startBarcodeConverter;
	}

system("pause");
return 0;
}
@ppate215

You really should try to stay away from using 'goto' command. It create spaghetti code. Hard to follow, and prone to mistakes. Anyway, your 'stray' code comes from line 135. The program just doesn't understand it. So, I removed it. Also made for loops for lines 60 and 83, and a do/while loop to allow for continuing with another zipcode or stopping.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>


using std::cout;
using std::cin;
using std::endl;
using std::right;
using std::string;


string int2Code(int i)
{
string result = "";
switch(i) // switching between accepted digits depending on the given parameter
{
case 1:
result = ":::||";
break;
case 2:
result = "::|:|";
break;
case 3:
result = "::||:";
break;
case 4:
result = ":|::|";
break;
case 5:
result = ":|:|:";
break;
case 6:
result = ":||::";
break;
case 7:
result = "|:::|";
break;
case 8:
result = "|::|:";
break;
case 9:
result = "|:|::";
break;
case 0:
result = "||:::";
break;
default: // in case the argument didn’t fall in any of the above cases
result = ""; // means failure

}

// returning the barcode of the given int parameter
return result;
}



string checkDigit(int zipCode)
{
int result = 0;
int base = zipCode;

for(int code=5;code>0;code--)
{

	result += base%10;
	base/=10;
}

result = 10-(result%10);

return int2Code(result); //returns empty string if the conversion fails
}


string encodePrefix(int zipCode)
{

	string result = "";

	int base = zipCode;

	for(int code=5;code>0;code--)
	{

		result = int2Code(base%10) + " " + result;
		base/=10;
	}

	return result;
}

string encode(int zipCode)
{

	string prefix = "|" + encodePrefix(zipCode);

	string sufix = checkDigit(zipCode)+"|";

// returns the zip code using the postnet barcode representation
	return (prefix + sufix);
}


template <class T>
bool fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
{
	std::istringstream iss(s);
	return !(iss >> f >> t).fail();
}

int main(int argc, char *argv[])
{
	string str,answer="y",underline(37,'_');
	int zipCode;

	do
{

	if(argc == 2)
		str = argv[1];
	else
	{

		cout << "Enter zip code : ";
		cin >> str;
	}


	if(fromString<int>(zipCode, std::string(str), std::dec) && str.length()==5)
	{
		string code = encode(zipCode);
		cout << "This is your barcode-> " << code << endl;
		cout << "\t\t       " << underline << endl;
		cout << "\nConvert another zip code? (y/n) : ";
		cin >> answer;
	
	}
	else
	{
		cout << "Your zip code is not formatted correctly" << endl;
	
	}
}while(answer=="y");
system("pause");
return 0;
}
Your check digit function is over complicated for checking valid zip codes and also doesn't catch all incorrect zip codes ex. 111111. All you are looking for is that the zip code is five digits long, so something like this:

1
2
3
4
bool isCorrectZipCode(int zipCode) {
	int ans = zipCode / pow(10, 5);
	return ans > 0 && ans < 10;
}


I don't see anything else wrong with the code other than the goto statement. Not only is it a hackish and ugly way of implementing a loop, but it is also placed in a position that will make the program go into an infinite loop if the user gives the program an incorrect zip code from the command line.
Last edited on
Topic archived. No new replies allowed.