Outputting a zip code as a bar code format

Hello,

I've been asked to write a program that outputs a mailing label for a given address. The zip code needs to displayed as a "bar code" at the bottom of the mailing label. Example, the number 1 = :::|| and a 5 = :|:|: so on and so forth.

I can't figure out how to get the zip code number to store as the bar code characters. You'll see on line 74 that I tried to start with just one number, a 5 because the zip code I'm testing with ends with a 5. I figured if I could get that to work I'd be able to fill in the rest.

P.S. I couldn't get anything to show up when I previewed this message so I don't know if my "code format" worked. I manually typed the code inside the brackets.

Here's the 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
/*Josh Krynock CSC 134 80
March 21, 2016
This program prompts the user for their address info
and then creates a check code from the zip code. The zip code is converted to
a barcode and displayed in a printing label.
For extra credit a random zip code generator can be included. 
*/

#include <iostream>
#include <iomanip> //THis is to format the outputs. 
#include <cstdlib> //This is for generating random zip codes. 
using namespace std;

int main()
{
	//Variables for string input
	string name, street, city, state;
	// Variables for the zip code math. 
	int zip, check,
		ones,
		tens, tensA, 
		hund, hundA, 
		thou, thouA, 
		tenK, tenKA, 
		sum;
	
	//This section informs the user of the program's intent. 
	//It then prompts the user for their information. 
	
	cout << "Hello, User. This program will generate ";
	cout << "a mailing label for a given user's address. \n";
	cout << "Please enter the following information.\n";
	cout << "What is your name? ";
	getline(cin, name);
	cout << "What is your street address? ";
	getline(cin, street);
	cout << "What is your city? ";
	getline(cin, city);
	cout << "What is your state? ";
	getline(cin, state);
	cout << "What is your zip code? ";
	cin >> zip;
	cout << "\n";
	
	//This part of the program breaks the zipcode into single digits.
	//First the ones, 
	 
	ones = zip % 10;
	
	//then tenths
	tens = zip % 100; 
	tensA = (tens - ones) / 10;
	
	//Hundreths
	hund = zip % 1000;
	hundA = (hund - tens) / 100;
	
	//Thousandths
	thou = zip % 10000;
	thouA = (thou - hund) / 1000;
	
	//Ten K
	tenK = zip % 100000;
	tenKA = (tenK - thou) / 10000;	
	 
	//This part of the program calculates the check number. 
	check = (ones - tensA + hundA + thouA + tenKA);
	check %= 10;
	check = (10-check);
	
	//This part of the program modifies the zip code to display 
	//it in true bar code form. 
	
	if ( ones == 5 )
		ones = :|:|:;
		
	//This part of the program displays the postage label.
	cout << "\n\n\n\n";
	cout << left << "***********************************Postage\n\n";
	cout << name << "\n";
	cout << street << "\n";
	cout << city << " " << state << " " << zip << "\n\n";
	cout << "|" << tenKA << " " << thouA << " " << hundA;
	cout << " " << tensA << " " << ones << " " << check << "|";
	
	

	return 0;
}
If anyone else reads this that needs to know how I solved the problem, I added the #include<string> header file and then converted the values of the individual zip code digits into strings of colons and vertical bars to display. I'll copy the code below (partially) in case you need to read it.

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
	//This part of the program modifies the zip code to display 
	//it in true barcode form. 
	//These are the variables to store the bar code.
	string bar1, bar10, bar100, bar1k, bar10k, barcheck;
	
	//This section converts the ones value of zip code.
	if (ones == 1)
		bar1 = ":::||";
	else if (ones == 2)
		bar1 = "::|:|";
	else if (ones == 3)
		bar1 = "::||:";
	else if (ones == 4)
		bar1 = ":|::|";
	else if (ones == 5)
		bar1 = ":|:|:";
	else if (ones == 6)
		bar1 = ":||::";
	else if (ones == 7)
		bar1 = "|:::|";
	else if (ones == 8)
		bar1 = "|::|:";
	else if (ones == 9)
		bar1 = "|:|::";
	else if (ones == 0)
		bar1 == "||:::";
You may put it in an array:

std::string zip_array[] = { "||:::", ":::||", "::|:|", ... };

This can be use to create a string:

std::string result = zip_array[tenK] + zip_array[thouA] + ...;
So I read yesterday in another thread that the std: at the begining of the line can be omitted if (which I do) you have the line: using namespace std;

So, using your code I could just type :string zip_array or string zip_array?

Also, I have not read about arrays in class yet so I'm unfamiliar with all that goes into making them. I think we were limited to using only techniques we'd covered. But I am interested.

If you wouldn't mind, can I ask two questions? Secondly, do I understand correctly that the array is written so that the first "||:::" corresponds to the 1, so on and so forth? So that in the result line displays the corresponding string for the value of the variable in the brackets?
So I read yesterday in another thread that the std: at the begining of the line can be omitted if (which I do) you have the line: using namespace std;

That's correct. However the use of using namespace std; is generally not recommended. Goggle it for the reasons why. We generally don't raise an issue about its use for beginners, but it's still a good habit to get into to avoid its use.

I could just type :string zip_array or string zip_array?

The first form (with the single colon) is not legal. The second form is just fine if you have using namespace std;

do I understand correctly that the array is written so that the first "||:::" corresponds to the 1

Almost. It corresponds to the 0th entry, not 1.

So that in the result line displays the corresponding string for the value of the variable in the brackets?

Yes.




Thank you a ton AbstractionAnon!
Topic archived. No new replies allowed.