Zipcode

I'm having problems with the output of my zip code. I want it to be 32 characters long. The string I have put in for the beginning of the output won't appear. Also, the output is too short. Any solutions to the code?


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

string bc(int digit) {
if (digit == 9) {
cout << "|:|::";
}
if (digit == 8) {
cout << "|::|:";
}
if (digit == 7) {
cout << "|:::|";
}
if (digit == 6) {
cout << ":||::";
}
if (digit == 5) {
cout << ":|:|:";
}
if (digit == 4) {
cout << ":|::|";
}
if (digit == 3) {
cout << "::||:";
}
if (digit == 2) {
cout << "::|:|";
}
if (digit == 1) {
cout << ":::||";
}
if (digit == 0) {
cout << "||:::";
}

return "";
}
string bar_code(int zipcode) {

string output = "|";


int dig1, dig2, dig3, dig4, dig5;
int sum_dig;




dig1 = zipcode / 10000;
zipcode = zipcode % 10000;
output = output + bc(dig1);

dig2 = zipcode % 1000;
zipcode = zipcode % 1000;
output = output + bc(dig2);

dig3 = zipcode % 100;
zipcode = zipcode / 100;
output = output + bc(dig3);

dig4 = zipcode /10;
zipcode = zipcode % 10;
output = output + bc(dig4);

dig5 = zipcode / 1;
zipcode = zipcode / 1;
output = output + bc(dig5);



int sum = dig1 + dig2 + dig3 + dig4 + dig5;
int extra = sum == 0?0 : 10-sum;

output = output + bc(extra);

output = output + "|";

return output;
}

int main(){

char ctn;

do {
int zipcode;

cout << "Please enter a zip code: ";
cin >> zipcode;

if (cin.fail()) {
cout << "zip code you entered does not exist. ";
return 1;
}

if (zipcode < 0 || zipcode >= 100000) {
cout << "zip code you entered does not exist";
return 1;
}
if (zipcode > 0 || zipcode <= 99999) {
cout << "Barcode = " << bar_code(zipcode) << endl;
}

cout << endl;

cout << "Continue (y/n)? ";
cin >> ctn;
}
while ( ctn == 'y');

return 0;
}
Last edited on
Why does your function bc always return "" regardless of the input.

An easier solution would be to read the input as a string and loop through easier character.
I want it to be 32 characters long

How should it work?
Imagine the input is 12345 which would produce a string with the length of 25.
What should the other 7 characters be?
Topic archived. No new replies allowed.