Zipcode -> Bar Code proj C++

dubstar

format c:
Joined
3 Dec 2002
Messages
1,357
I need to use modulus to individualize the numbers to create a barcode based on each number.

I'm want to write out

int barcode;
int digit1;
int digit2;
int digit3;
int digit4;
int digit5;

string 1;
string 2;
string 3;
string 4;
string 5;
string 6;
string 7;
string 8;
string 9;
string 0;


where those strings are the pre-assigned barcode segments, and based on which number is input, a set of long and short bars are displayed.

i've been trying to do


digit5 = 10 % zipcode;
digit4 = 100 % zipcode;
digit3 = 1000 % zipcode; ... etc

but how can i easily implement a way to tie the barcode string to the result of the modulus without a billion copy&pastes of if/else?

can i have a variable that cycles through the digits? like digit1++ which results in digit2?



-- does this make sense?
 
I'd use commas and strip all those int's and strings into one line. I'd also use better variable names as you cant have numbers as variables.
 
What's the reason for creating so many strings? Are they going to be the output of something? I'm not sure if the tie into the other code somehow.

digit1++ would only add 1 to the value of digit1, but would not move you to the variable digit2. You could probably store your values in a single row array. You could use a for or if loop to cycle through each column until it reached the end.
 
Code:
[SIZE=2][SIZE=2]#include <iostream>[/SIZE]
 
[SIZE=2]using std::cout;[/SIZE]
[SIZE=2]using std::endl;[/SIZE]
 
[SIZE=2]int main(void)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]   int code = 50360218;[/SIZE]
 
[SIZE=2]   int result = code %10;[/SIZE]
[SIZE=2]   code = code /10;[/SIZE]
 
[SIZE=2]    while(code>=1)[/SIZE]
[SIZE=2]    {[/SIZE]
[SIZE=2]         cout << result << endl; [/SIZE]
[SIZE=2]         result=code%10;[/SIZE]
[SIZE=2]         code=code/10; [/SIZE]
[SIZE=2]    }[/SIZE]
[SIZE=2]    cout << result << endl;[/SIZE]
[SIZE=2]    return 0;[/SIZE]
[SIZE=2]}[/SIZE]
[/SIZE]
You can use this to break down an integer and retrieve its digits.

You could make an array of strings and use the digit retrieved from the above code to represent that index in the array.

Like, for string at array index 0, put the bar code string information you need for digit 0.

When you get a digit 0 from the integer, reference the 0-index element in the array and print it.

Something like that. Other posters may be able to improve on my logic, but this should get you started.
 
Last edited:
Code:
[SIZE=2][COLOR=#008000]/*[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]******************************************[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]** Program name: *****[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]*** Date: Feb 02 2007 ****[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]**** Author: Alan Rosenquist ***[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]***** Description: **[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]******************************************[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]*/[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]// Library[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]<string>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][SIZE=2] std;[/SIZE]
 
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] main()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#008000]// Header[/COLOR][/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t ****** * * ******* ***** ******* "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t* * * * * * * "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t* * * * * ***** * "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t* * * * * * * * "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t ****** ****** ******* ****** * "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t * Program name: Zipcode to Barcode Converter"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t * Date: March 23th 2007"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t * Author: Alan Rosenquist"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]" \t * Description: Converts Zipcodes to Bar codes for mailing."[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][SIZE=2]<< endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
 
 
[SIZE=2][COLOR=#008000]// Declaration of[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] zipcode=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2]string one; [/SIZE][SIZE=2][COLOR=#008000]// :::||[/COLOR][/SIZE]
[SIZE=2]string two; [/SIZE][SIZE=2][COLOR=#008000]// ::|:|[/COLOR][/SIZE]
[SIZE=2]string three; [/SIZE][SIZE=2][COLOR=#008000]// ::||:[/COLOR][/SIZE]
[SIZE=2]string four; [/SIZE][SIZE=2][COLOR=#008000]// :|::|[/COLOR][/SIZE]
[SIZE=2]string five; [/SIZE][SIZE=2][COLOR=#008000]// :|:|:[/COLOR][/SIZE]
[SIZE=2]string six; [/SIZE][SIZE=2][COLOR=#008000]// :||::[/COLOR][/SIZE]
[SIZE=2]string seven; [/SIZE][SIZE=2][COLOR=#008000]// |:::|[/COLOR][/SIZE]
[SIZE=2]string eight; [/SIZE][SIZE=2][COLOR=#008000]// |::|:[/COLOR][/SIZE]
[SIZE=2]string nine; [/SIZE][SIZE=2][COLOR=#008000]// |:|::[/COLOR][/SIZE]
[SIZE=2]string zero; [/SIZE][SIZE=2][COLOR=#008000]// ||:::[/COLOR][/SIZE]
[SIZE=2]string bar1; [/SIZE][SIZE=2][COLOR=#008000]// first set of bars[/COLOR][/SIZE]
[SIZE=2]string bar2; [/SIZE][SIZE=2][COLOR=#008000]// second set of bars[/COLOR][/SIZE]
[SIZE=2]string bar3; [/SIZE][SIZE=2][COLOR=#008000]// third set of bars[/COLOR][/SIZE]
[SIZE=2]string bar4; [/SIZE][SIZE=2][COLOR=#008000]// fourth set of bars[/COLOR][/SIZE]
[SIZE=2]string bar5; [/SIZE][SIZE=2][COLOR=#008000]// fifth set of bars[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] digit1=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] digit2=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] digit3=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] digit4=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] digit5=[/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2](0);[/SIZE]
[SIZE=2]string bars;[/SIZE]
[SIZE=2][COLOR=#008000]// Giving String Value to the numbers[/COLOR][/SIZE]
[SIZE=2]one = [/SIZE][SIZE=2][COLOR=#a31515]":::||"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]two = [/SIZE][SIZE=2][COLOR=#a31515]"::|:|"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]three = [/SIZE][SIZE=2][COLOR=#a31515]"::||:"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]four = [/SIZE][SIZE=2][COLOR=#a31515]":|::|"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]five = [/SIZE][SIZE=2][COLOR=#a31515]":|:|:"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]six = [/SIZE][SIZE=2][COLOR=#a31515]":||::"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]seven = [/SIZE][SIZE=2][COLOR=#a31515]"|:::|"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]eight = [/SIZE][SIZE=2][COLOR=#a31515]"|::|:"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]nine = [/SIZE][SIZE=2][COLOR=#a31515]"|:|::"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]zero = [/SIZE][SIZE=2][COLOR=#a31515]"||:::"[/COLOR][/SIZE][SIZE=2];[/SIZE]
 
 
 
 
[SIZE=2][COLOR=#008000]// Input[/COLOR][/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"Please enter the Bar Code \t"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cin >> zipcode;[/SIZE]
 
 
 
 
[SIZE=2][COLOR=#008000]// Process[/COLOR][/SIZE]
[SIZE=2]digit5 = (zipcode % 10);[/SIZE]
[SIZE=2]digit4 = ((zipcode % 100) - (digit5));[/SIZE]
[SIZE=2]digit3 = ((zipcode % 1000) - (digit5 + digit4)) / 100;[/SIZE]
[SIZE=2]digit2 = ((zipcode % 10000) - (digit5 + digit4 + digit3)) / 1000;[/SIZE]
[SIZE=2]digit1 = ((zipcode % 100000) - (digit5 + digit4 + digit3 + digit2)) / 10000;[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 1)[/SIZE]
[SIZE=2]bar1 = one;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 2)[/SIZE]
[SIZE=2]bar1 = two;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 3)[/SIZE]
[SIZE=2]bar1 = three;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 4)[/SIZE]
[SIZE=2]bar1 = four;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 5)[/SIZE]
[SIZE=2]bar1 = five;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 6)[/SIZE]
[SIZE=2]bar1 = six;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 7)[/SIZE]
[SIZE=2]bar1 = seven;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 8)[/SIZE]
[SIZE=2]bar1 = eight;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (digit1 == 9)[/SIZE]
[SIZE=2]bar1 = nine;[/SIZE]
[SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]bar1 = zero;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
 
 
 
 
[SIZE=2][COLOR=#008000]// Output[/COLOR][/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"This is the barcode for the zipcode entered. "[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"||"[/COLOR][/SIZE][SIZE=2] << digit1 << digit2 << digit3 << digit4 << digit5 << [/SIZE][SIZE=2][COLOR=#a31515]"||"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"||"[/COLOR][/SIZE][SIZE=2] << bar1 << bar2 << bar3 << bar4 << bar5 << [/SIZE][SIZE=2][COLOR=#a31515]"||"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
 
 
 
 
[SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
[SIZE=2]}[/SIZE]

ok, i'm good up until IF/ELSE... is there a way to use WHILE for the same affect? It works perfectly, and i could just repeat IF/ELSE for the rest of the digits, but my teacher said he wants WHILE and i think he said SWITCH too. maybe a FOR...

so .. i've been racking my brain for a while... maybe my whole strategy is wacked out. If someone could just theorize with me, i'd feel better than getting straight up code.

thanks for the replies too guys.



note: the formatting is taken away when i copy and paste the code over here.. if there is a better way to maintain it, please include it in a post.
 
Last edited:
Hi there!

I have a similar programming task I have to complete, however I have been instructed to put the barcodes into a "classäny suggestions on how I should do that?
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back