Enter bar code and get zip code?

I was looking online online and found a code very similar to this one in the sense that if you enter a zip code it gives you the postal bar code of the zip code. However, I am needing to re-arrange this code so that if I enter the bar code it gives me the zip code. I also need it to print out a message whenever they enter a bar code that is invalid. Any suggestions, ideas, or does anyone have a solution?

Thanks in advance


#include<iostream>
using namespace std;

//This Function Takes one digit at a time and returns the assosiated barcode string
string Bar_Code(char digit)
{
if (digit == '0')
return "||:::";
else if (digit == '1')
return ":::||";
else if (digit == '2')
return "::|:|";
else if (digit == '3')
return "::||:";
else if (digit == '4')
return ":|::|";
else if (digit == '5')
return ":|:|:";
else if (digit == '6')
return ":||::";
else if (digit == '7')
return "|:::|";
else if (digit == '8')
return "|:::|";
else if (digit == '9')
return "|:|::";
else
return "Invalid";

}

int main()
{
//Declaration
string user_input;
string result;

//Take Input the number as a string :) This will make it easy to work on individual digits
cout << "Enter Number: ";
cin >> user_input;

//Now Pass Digits One by One
for(int i=0;i<user_input.length();i++)
{
//Storing barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
string current_barcode = Bar_Code(user_input[i]);

//The Line Below Will Print Each Digit present in user_input
//cout << user_input[i] << endl;

//Checking for invalid digit -- Because they are characters anyone can enter other than a number
if(current_barcode.compare("Invalid") == 0)
{
cout << "The Number You Entered in Invalid!" << endl;
break;
}

//Concatenating the barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
result = result + current_barcode;
}

//Printing Result
cout << result;

cout << endl;
cin.ignore();
return 0;
}
Last edited on
geez dude please use code tags:
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
#include<iostream>
using namespace std;

//This Function Takes one digit at a time and returns the assosiated barcode string
string Bar_Code(char digit)
{
if (digit == '0')
return "||:::";
else if (digit == '1')
return ":::||";
else if (digit == '2')
return "::|:|";
else if (digit == '3')
return "::||:";
else if (digit == '4')
return ":|::|";
else if (digit == '5')
return ":|:|:";
else if (digit == '6')
return ":||::";
else if (digit == '7')
return "|:::|";
else if (digit == '8')
return "|:::|";
else if (digit == '9')
return "|:|::";
else
return "Invalid";

}

int main()
{
//Declaration
string user_input;
string result;

//Take Input the number as a string :) This will make it easy to work on individual digits
cout << "Enter Number: ";
cin >> user_input;

//Now Pass Digits One by One
for(int i=0;i<user_input.length();i++)
{
//Storing barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
string current_barcode = Bar_Code(user_input[i]);

//The Line Below Will Print Each Digit present in user_input
//cout << user_input[i] << endl;

//Checking for invalid digit -- Because they are characters anyone can enter other than a number
if(current_barcode.compare("Invalid") == 0)
{
cout << "The Number You Entered in Invalid!" << endl;
break;
}

//Concatenating the barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
result = result + current_barcode;
}

//Printing Result
cout << result;

cout << endl;
cin.ignore();
return 0;
}
Im a very basic c++ user, is there a way that I can type in the bar code and return the zip code?
Topic archived. No new replies allowed.