Zipcode -> Bar Code proj C++

dubstar

format c:
Joined
Dec 3, 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?
 

LordOfLA

Godlike!
Joined
Feb 2, 2004
Messages
7,026
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.
 

Luna

OSNN Senior Addict
Joined
Jan 6, 2004
Messages
303
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.
 

albybum

Penguin Rancher
Joined
Feb 9, 2002
Messages
280
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:

dubstar

format c:
Joined
Dec 3, 2002
Messages
1,357
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:

Trikky73

OSNN Newbie
Joined
Apr 5, 2007
Messages
2
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Threads
61,976
Messages
673,272
Members
89,046
Latest member
osnnforumtime