#include <iostream> #include <iomanip> #include <string> using namespace std; in
ID: 3833417 • Letter: #
Question
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int barTocode(string bCode)
{
if (bCode == "||:::")
{
return 0;
}
else if (bCode == ":::||")
{
return 1;
}
else if (bCode == "::|:|")
{
return 2;
}
else if (bCode == "::||:")
{
return 3;
}
else if (bCode == ":|::|")
{
return 4;
}
else if (bCode == ":|:|:")
{
return 5;
}
else if (bCode == ":||::")
{
return 6;
}
else if (bCode == "|:::|")
{
return 7;
}
else if (bCode == "|::|:")
{
return 8;
}
else if (bCode == "|:|::")
{
return 9;
}
else
{
return -1;
}
}
int convertor(string barcode)
{
const int barlength = 5;
if (barcode.substr(0, 1) != "|")
{
return -1;
}
if (barcode.substr(barcode.length() - 1, 1) != "|")
{
return -1;
}
int b1 = barTocode(barcode.substr(1, barlength));
int b2 = barTocode(barcode.substr(6, barlength));
int b3 = barTocode(barcode.substr(11, barlength));
int b4 = barTocode(barcode.substr(16, barlength));
int b5 = barTocode(barcode.substr(21, barlength));
int Zip_code = 0;
Zip_code = b1 * 10000 + b2 * 1000 + b3 * 100 + b4 * 10 + b5;
return Zip_code;
}
int main()
{
string input_barcode;
cout << "Enter a postal bar code: ";
cin >> input_barcode;
int Z = convertor(input_barcode);
if (Z >= 0)
{
cout << "The zip code is " << Z << endl;
}
else
{
cout << "The code entered is invalid." << endl;
}
cin.get();
return 0;
}
********
when I try Sample trial 1: |:|::|:|::|:::||||::::||::|:::||
i got 44106. but as you can see, I supposed to get invalid result.
please, help me to fix my error..
Implementation details: You should separate your code into functions. Your program should contain at least two functions (aside from main() that do the foll 1. One function should take a 5-bar block as parameter and return its corresponding digit (or return a different integer if there is a 2. Another function should take in the entire string of bars as parameter and return the 5-digit zip code, or return a different integ Think about the possible errors that could occur in the entry of a bar code: The correct number of bars must be entered. If not, it is an error. The first and last bars must be long frame bars. Ifeither is a short bar, it is an error. The check digit must cause the sum to be a multiple of 10. lfnot, it is an error. If any 5-bar block does not correspond to a number, it is an error. Some trial runs are provided below. As usual, user input is given in red. Sample trial 1: Enter a postal bar code I:l: l l I IIII Il I Il The code entered is invalid. Sample trial 2 Enter a postal bar code II I II: l I l l l I: l l l The zip code is 90024. Sample trial 3: Enter a postal bar code I: l l l I I l I I::: l I I The zip code is 21367Explanation / Answer
Here is the update for you:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int barTocode(string bCode)
{
if (bCode.compare("||:::") == 0)
{
return 0;
}
else if (bCode.compare(":::||") == 0)
{
return 1;
}
else if (bCode.compare("::|:|") == 0)
{
return 2;
}
else if (bCode.compare("::||:") == 0)
{
return 3;
}
else if (bCode.compare(":|::|") == 0)
{
return 4;
}
else if (bCode.compare(":|:|:") == 0)
{
return 5;
}
else if (bCode.compare(":||::") == 0)
{
return 6;
}
else if (bCode.compare("|:::|") == 0)
{
return 7;
}
else if (bCode.compare("|::|:") == 0)
{
return 8;
}
else if (bCode.compare("|:|::") == 0)
{
return 9;
}
else
{
return -1;
}
}
int convertor(string barcode)
{
const int barlength = 5;
if (barcode.substr(0, 1) != "|")
{
return -1;
}
if (barcode.substr(barcode.length() - 1, 1) != "|")
{
return -1;
}
if(barcode.length() != 27) //This is what you're missing. If the input length is more than 5 blocks
return -1; //Also return a warning.
int b1 = barTocode(barcode.substr(1, 5));
int b2 = barTocode(barcode.substr(6, 5));
int b3 = barTocode(barcode.substr(11, 5));
int b4 = barTocode(barcode.substr(16, 5));
int b5 = barTocode(barcode.substr(21, 5));
int Zip_code = 0;
Zip_code = b1 * 10000 + b2 * 1000 + b3 * 100 + b4 * 10 + b5;
return Zip_code;
}
int main()
{
string input_barcode;
cout << "Enter a postal bar code: ";
cin >> input_barcode;
int Z = convertor(input_barcode);
if (Z >= 0)
{
cout << "The zip code is " << Z << endl;
}
else
{
cout << "The code entered is invalid." << endl;
}
cin.get();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.