int string_to_num(string code) { // I have this to convert binary to a digit
if (code == "||:::") {
return 0;
}
else if (code == ":::||") {
return 1;
}
else if (code == "::|:|") {
return 2;
}
else if (code == "::||:") {
return 3;
}
else if (code == ":|::|") {
return 4;
}
else if (code == ":|:|:") {
return 5;
}
else if (code == ":||::") {
return 6;
}
else if (code == "|:::|") {
return 7;
}
else if (code == "|::|:") {
return 8;
}
else if (code == "|:|::") {
return 9;
}
else {
return -1;
}
}
int convert(string barcode) {
const int DIGIT_LENGTH = 5; //setting the parameters of the digit length
if (barcode.substr(0, 1) != "|") { //testing for | at beginning
return -1;
}
if (barcode.substr(barcode.length() - 1, 1) != "|") { //testing for |
return -1;
}
int d1 = string_to_num(barcode.substr(1, DIGIT_LENGTH)); //first digit
int d2 = string_to_num(barcode.substr(6, DIGIT_LENGTH));
int d3 = string_to_num(barcode.substr(11, DIGIT_LENGTH));
int d4 = string_to_num(barcode.substr(16, DIGIT_LENGTH));
int d5 = string_to_num(barcode.substr(21, DIGIT_LENGTH)); //last digit
int check = (d1 + d2 + d3 + d4 + d5) % 10;
int zip = 0;
zip = ; //Is this how i go about getting the zip?
return zip;
}
int main(int argc, char **argv)
{
string input;
cin >> input;
int v = convert(input);
if (v >= 0){
cout << v << endl;
} else{
cout << "The postal bar code is invalid." << endl;
}
return 0;
}
When posting code, please use code tags. Highlight the code and press the <> button to the right of the edit window.
To form the zipcode, just do the math: zipcode = d1*10000+d2*1000+d3*100+d4*10+d5;
Notethat if you print the zipcode, you may need to ensure that you have leading zeros for a code like 08505.