Good morning all, I am having a little trouble with getting the desired output from my program that converts numeric zip codes into their postal bar code equivalents. My code is below as well as my input along with current and desired outputs. Thank you in advance.
Input : 95003
Current output: ||:|:::|:|:||:::||:::::||:|
Desired output: ||:|:::|:|:||:::||:::::||:::||:|
The current output looks correct for 95003 going by what you have in the if..else statements. The desired output seems to have an extra 3 at the end (950033).
Thank you for the reply wildblue. I forgot a valuable piece of information, the last digit is supposed to be a check digit, that when the sum of the zip code digits is computed, the check digit is supposed to be the closest number to this sum that creates a multiple of 10. In my example, 95003, 9+5+0+0+3 = 17, so the check digit would be 3. I would need my program to determine this check digit value and add the appropriate number to the output in bar code form. This has me stumped.
Ok so I have been working with a new code and am very close to having the program running, the only issue is that the check digit is being computed incorrectly. I know this code is a mess and could be cleaned up a bunch but I just need this check digit to output correctly, everything else works perfectly.
#include <iostream>
#include <string>
int char_to_int( char c ) // convert char to int ( '7' to 7 etc. )
{
// '1' - '0' == 1, '4' - '0' == 4, '8' - '0' == 8 etc.
if( std::isdigit(c) ) return c - '0' ;
elsereturn 10 ; // error, invalid
}
int check_digit( std::string str )
{
int sum = 0 ;
// sum up the numeric value of all the digits
for( unsignedint i = 0 ; i < str.size() ; ++i ) sum += char_to_int( str[i] ) ;
constint remainder = sum%10 ; // remainder when divided by 10
if( remainder == 0 ) return 0 ; // already a multiple of 10
elsereturn 10 - remainder ; // closest number to this sum that creates a multiple of 10.
}
const std::string invalid = "Invalid" ; // avoid hard-coded literals used in more than one place
std::string barCode( char digit )
{
// we can use a lookup table to get the bar code
staticconst std::string codes[] =
{
"||:::", // 0
":::||", // 1
"::|:|", // 2
"::||:", // 3
":|::|", // 4
":|:|:", // 5
":||::", // 6
"|:::|", // 7
"|::|:", // 8
"|:|::", // 9
invalid // invalid
};
// convert char digit to its int value, and lookup in the table
return codes[ char_to_int(digit) ] ;
}
int main()
{
std::string zipCode;
std::string result;
std::cout << "Please enter a zip code: ";
std::cin >> zipCode;
for ( unsignedint i = 0; i < zipCode.length(); i++ )
{
const std::string currentBarcode = barCode ( zipCode[i] );
if ( /*currentBarcode.compare ("Invalid") == 0*/ currentBarcode == invalid )
{
std::cout << "You have entered a number that is not valid\n" ; // << endl;
return 1 ; // break;
}
result += currentBarcode ;
}
// add check digit as the last digit
char last_digit = check_digit(zipCode) + '0' ; // convert to a char: 3 + '0' == '3' etc
result = result + barCode(last_digit);
std::cout << "|" << result << "|\n";
}