Good morning everyone I am currently trying to complete my code that converts zip codes to bar codes. I need input on how to properly create the last digit which is a check digit. for the check digit, you must add up each individual value of the zip code and add a check digit that makes that value into a multiple of 10. for example 90210 is 9+0+2+1+0 = 12 so the check digit would be 8 to make a value of 20. This code functions great except that it improperly calculates the check digit, what am I missing here? I am completely stuck here and any advice would be great.
Code
#include <iostream>
#include <string>
int error;
char a [] = ":::||";
char b [] = "::|:|";
char c [] = "::||:";
char d [] = ":|::|";
char e [] = ":|:|:";
char f [] = ":||::";
char g [] = "|:::|";
char h [] = "|::|:";
char i [] = "|:|::";
char j [] = "||:::";
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
int digit4 = 0;
int digit5 = 0;
int digit6 = 0;
int temp1 = 0;
int main()
{
\
std::cout << "Enter the zipcode! \n";
std::cin >> temp;
//The initial if statement determines if the number entered has more than 5 digits and decides which procedure to use
if (temp / 10000 > 9){
temp1 = temp;
//Set each digit to a seperate variable
if (temp % 10 > 0){
do{
(temp1 = temp1 / 10);
}
while (temp1 / 10 > 0);
}
digit1 = temp1;
temp1 = temp;
if (temp % 10 > 0){
do{
(temp1 = temp1 / 10);
}
while (temp1 / 10 > 10);
}
digit2 = temp1 - (digit1 * 10);
temp1 = temp;
if (temp % 10 > 0){
do{
(temp1 = temp1 / 10);
}
while (temp1 / 10 > 100);
}
digit3 = temp1 - ((digit1 * 100)+(digit2*10));
temp1 = temp;
if (temp % 10 > 0){
do{
(temp1 = temp1 / 10);
}
while (temp1 / 10 > 1000);
}
digit4 = temp1 - ((digit1 * 1000)+(digit2*100)+(digit3*10));
temp1 = temp;
if (temp % 10 > 0){
do{
(temp1 = temp1 / 10);
}
while (temp1 / 10 > 10000);
}
digit5 = temp1 - ((digit1*10000)+(digit2*1000)+(digit3*100)+(digit4*10));
}else {
digit5 = temp % 10;
temp = temp / 10;
digit4 = temp % 10;
temp = temp / 10;
digit3 = temp % 10;
temp = temp / 10;
digit2 = temp % 10;
temp = temp / 10;
digit1 = temp % 10;}
//this portion checks to see if there are more than five digits and puts a boolean value into error accordingly
if ((temp / 10) > 0) error = 1;
else error = 0;
std::cout << "If the following value is the same as the first five digits of the value entered, the digit distribution worked properly.\n";
std::cout << digit1 << digit2 << digit3 << digit4 << digit5 << "\n";
//COnvert digits to barcodes
std::cout << "|";
if (digit1 == 1) std::cout << a;
else if (digit1 == 2) std::cout << b;
else if (digit1 == 3) std::cout << c;
else if (digit1 == 4) std::cout << d;
else if (digit1 == 5) std::cout << e;
else if (digit1 == 6) std::cout << f;
else if (digit1 == 7) std::cout << g;
else if (digit1 == 8) std::cout << h;
else if (digit1 == 9) std::cout << i;
else if (digit1 == 0) std::cout << j;
if (digit2 == 1) std::cout << a;
else if (digit2 == 2) std::cout << b;
else if (digit2 == 3) std::cout << c;
else if (digit2 == 4) std::cout << d;
else if (digit2 == 5) std::cout << e;
else if (digit2 == 6) std::cout << f;
else if (digit2 == 7) std::cout << g;
else if (digit2 == 8) std::cout << h;
else if (digit2 == 9) std::cout << i;
else if (digit2 == 0) std::cout << j;
if (digit3 == 1) std::cout << a;
else if (digit3 == 2) std::cout << b;
else if (digit3 == 3) std::cout << c;
else if (digit3 == 4) std::cout << d;
else if (digit3 == 5) std::cout << e;
else if (digit3 == 6) std::cout << f;
else if (digit3 == 7) std::cout << g;
else if (digit3 == 8) std::cout << h;
else if (digit3 == 9) std::cout << i;
else if (digit3 == 0) std::cout << j;
if (digit4 == 1) std::cout << a;
else if (digit4 == 2) std::cout << b;
else if (digit4 == 3) std::cout << c;
else if (digit4 == 4) std::cout << d;
else if (digit4 == 5) std::cout << e;
else if (digit4 == 6) std::cout << f;
else if (digit4 == 7) std::cout << g;
else if (digit4 == 8) std::cout << h;
else if (digit4 == 9) std::cout << i;
else if (digit4 == 0) std::cout << j;
if (digit5 == 1) std::cout << a;
else if (digit5 == 2) std::cout << b;
else if (digit5 == 3) std::cout << c;
else if (digit5 == 4) std::cout << d;
else if (digit5 == 5) std::cout << e;
else if (digit5 == 6) std::cout << f;
else if (digit5 == 7) std::cout << g;
else if (digit5 == 8) std::cout << h;
else if (digit5 == 9) std::cout << i;
else if (digit5 == 0) std::cout << j;
//Check digit calculation
if (error == 0){
int temporary = digit1 + digit2 + digit3 + digit4 + digit5;
int temporary1 = temporary;
if (temporary % 10 != 1){
do {
temporary1++;
}while (temporary1 % 10 != 1);
}
digit6 = temporary1 - temporary;
if (digit6 == 1) std::cout << a;
else if (digit6 == 2) std::cout << b;
else if (digit6 == 3) std::cout << c;
else if (digit6 == 4) std::cout << d;
else if (digit6 == 5) std::cout << e;
else if (digit6 == 6) std::cout << f;
else if (digit6 == 7) std::cout << g;
else if (digit6 == 8) std::cout << h;
else if (digit6 == 9) std::cout << i;
else if (digit6 == 0) std::cout << j;
}
else
std::cout << "error - you entered more than 5 digits \n";
//lastly, the closing | for the barcode
std::cout << "| \n";
return 0;
}
Please, research look up tables. When converting, lookup tables are a blessing, especially simple array structures.
To fetch the barcode for a letter:
Sorry to simplify your code, but it was annoying for me to read.
You can handle digits (as characters) in a similar manner:
If you want to handle integers,
/ 10
to shift the number to the right.% 10
to get the value of the right most digit.Put inside a
for
loop to handle many digits:Note that the above loop processes the digits in the reverse order. You could push them onto a stack and then pop them out to reverse their order.
I suggest treating the number as character digits, it's simpler.