zipcode to barcode conversion
Mar 7, 2016 at 10:59pm UTC
i need to write a program that asks the user for a zip code and prints the bar code. Use : (colon) for half bars and | (vertical bar) for full bars.
I'm extremely new at this so i did my best attempt at it but i'm stuck at this point and don't know how to proceed from here. its giving me error messages for the digits, saying they're uninitialized local variables. Here is what i have so far:
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
#include <iostream>
using namespace std;
#include <sstream>
#include <string>
string getBarcode(int );
string getBarcode(int digit)
{
switch (digit)
{
case 1: return ":::||" ;;
case 2: return "::|:|" ;;
case 3: return "::||:" ;;
case 4: return ":|::|" ;;
case 5: return ":|:|:" ;;
case 6: return ":||::" ;;
case 7: return "|:::|" ;;
case 8: return "|::|:" ;;
case 9: return "|:|::" ;;
case 0: return "||:::" ;;
default : return "Invalid" ;
}
}
int main()
{
int zipCode;
int digitA;
int digitB;
int digitC;
int digitD;
int digitE;
cout << "Please type your zip code: " ;
cin >> zipCode;
int sum;
string barCode = "|" , checkCode;
barCode += getBarcode(digitA);
barCode += getBarcode(digitB);
barCode += getBarcode(digitC);
barCode += getBarcode(digitD);
barCode += getBarcode(digitE);
sum = (digitA + digitB + digitC + digitD + digitE);
checkCode = getBarcode(sum);
cout << barCode + checkCode << endl;
return sum;
}
Mar 7, 2016 at 11:45pm UTC
This code which I just wrote doesn't check whether the ZIP entered is valid or not, but it calculates correctly the bar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include<iostream>
#include<string>
using namespace std;
string bars[10] = {"||:::" , ":::||" , "::|:|" , "::||:" , ":|::|" , ":|:|:" ,":||::" , "|:::|" ,"|::|:" ,"|:|::" }; //all bar codes
string getBar(int n){
return bars[n]; //return correct bar code
}
int char_to_int(char n){
int value = n - 48; //you'll see why it's needed
return value;
}
int main(){
cout<<"Enter ZIP:" ;
string zip; //here's our zip code
cin>>zip;
for (int i=0; i<zip.size(); i++){
char chr = zip[i]; //character for zip[i] example 9
int place = char_to_int(chr); //here character 9 becomes int 9
cout<<getBar(place); //returns 9 bar code
}
}
Topic archived. No new replies allowed.