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;
}
#include<iostream>
usingnamespace std;
//This Function Takes one digit at a time and returns the assosiated barcode string
string Bar_Code(char digit)
{
if (digit == '0')
return"||:::";
elseif (digit == '1')
return":::||";
elseif (digit == '2')
return"::|:|";
elseif (digit == '3')
return"::||:";
elseif (digit == '4')
return":|::|";
elseif (digit == '5')
return":|:|:";
elseif (digit == '6')
return":||::";
elseif (digit == '7')
return"|:::|";
elseif (digit == '8')
return"|:::|";
elseif (digit == '9')
return"|:|::";
elsereturn"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;
}