Hi!! so for my CS lab were supposed to encode a 5 digit zip code into bars (using : and |'s) then we have to have a check digit where were supposed to add up all the digits and chose a check digit that is closest to make the sum a multiple of 10. so as an example were given that the zip code is 95014 and it has a sum of 19 so our check digit would be 1 since 19 + 1 = 20. and then basically that check digit is added to the end of the barcode. So pretty much the barcode for 95014 should look like |:|::(9) :|:|:(5) ||:::(0) :::||(1) :|::|(4) :::||(1). I just put the number in there to show the bars for that number. You dont need the numbers in the code like i have shown there. So i have most of the code but im having "strays"(?) and it wont read my code. Please help me out. I think i have most of it but I just really need some new eyes looking at it and telling me what im doing wrong. I would greatly appreciate it. Thank you so much!!
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
usingnamespace std;
string int2Code(int i)
{
string result = "";
switch(i) // switching between accepted digits depending on the given parameter
{
case 1:
result = ":::||";
break;
case 2:
result = "::|:|";
break;
case 3:
result = "::||:";
break;
case 4:
result = ":|::|";
break;
case 5:
result = ":|:|:";
break;
case 6:
result = ":||::";
break;
case 7:
result = "|:::|";
break;
case 8:
result = "|::|:";
break;
case 9:
result = "|:|::";
break;
case 0:
result = "||:::";
break;
default: // in case the argument didn’t fall in any of the above cases
result = ""; // means failure
break;
}
// returning the barcode of the given int parameter
return result;
}
string checkDigit(int zipCode)
{
int result = 0;
int base = zipCode;
int code = 5;
while(code > 0)
{
result += base%10;
base = base/10;
code–-;
}
result = 10-(result%10);
return int2Code(result); //returns empty string if the conversion fails
}
string encodePrefix(int zipCode)
{
string result = "";
int base = zipCode;
int code = 5;
while(code > 0)
{
result = int2Code(base%10) + " " + result;
base = base/10;
code–-;
}
return result;
}
string encode(int zipCode)
{
string prefix = "|" + encodePrefix(zipCode);
string sufix = checkDigit(zipCode)+"|";
// returns the zip code using the postnet barcode representation
return (prefix + sufix);
}
template <class T>
bool fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main(int argc, char *argv[])
{
string str;
int zipCode;
startBarcodeConverter:
if(argc == 2)
str = argv[1];
else
{
cout << "Enter zip code : ";
cin >> str;
}
if(fromString<int>(zipCode, std::string(str), std::dec) && str.length()==5)
{
string code = encode(zipCode);
cout << "This is your barcode-> " << code << endl;
cout << "————————————————–" << endl;
string answer;
cout << "\nConvert another zip code? (y/n) : ";
cin >> answer;
if(answer == "yes" || answer == "y")
goto startBarcodeConverter;
}
else
{
cout << "Your zip code is not formatted correctly" << endl;
goto startBarcodeConverter;
}
system("pause");
return 0;
}
You really should try to stay away from using 'goto' command. It create spaghetti code. Hard to follow, and prone to mistakes. Anyway, your 'stray' code comes from line 135. The program just doesn't understand it. So, I removed it. Also made for loops for lines 60 and 83, and a do/while loop to allow for continuing with another zipcode or stopping.
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::right;
using std::string;
string int2Code(int i)
{
string result = "";
switch(i) // switching between accepted digits depending on the given parameter
{
case 1:
result = ":::||";
break;
case 2:
result = "::|:|";
break;
case 3:
result = "::||:";
break;
case 4:
result = ":|::|";
break;
case 5:
result = ":|:|:";
break;
case 6:
result = ":||::";
break;
case 7:
result = "|:::|";
break;
case 8:
result = "|::|:";
break;
case 9:
result = "|:|::";
break;
case 0:
result = "||:::";
break;
default: // in case the argument didn’t fall in any of the above cases
result = ""; // means failure
}
// returning the barcode of the given int parameter
return result;
}
string checkDigit(int zipCode)
{
int result = 0;
int base = zipCode;
for(int code=5;code>0;code--)
{
result += base%10;
base/=10;
}
result = 10-(result%10);
return int2Code(result); //returns empty string if the conversion fails
}
string encodePrefix(int zipCode)
{
string result = "";
int base = zipCode;
for(int code=5;code>0;code--)
{
result = int2Code(base%10) + " " + result;
base/=10;
}
return result;
}
string encode(int zipCode)
{
string prefix = "|" + encodePrefix(zipCode);
string sufix = checkDigit(zipCode)+"|";
// returns the zip code using the postnet barcode representation
return (prefix + sufix);
}
template <class T>
bool fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main(int argc, char *argv[])
{
string str,answer="y",underline(37,'_');
int zipCode;
do
{
if(argc == 2)
str = argv[1];
else
{
cout << "Enter zip code : ";
cin >> str;
}
if(fromString<int>(zipCode, std::string(str), std::dec) && str.length()==5)
{
string code = encode(zipCode);
cout << "This is your barcode-> " << code << endl;
cout << "\t\t " << underline << endl;
cout << "\nConvert another zip code? (y/n) : ";
cin >> answer;
}
else
{
cout << "Your zip code is not formatted correctly" << endl;
}
}while(answer=="y");
system("pause");
return 0;
}
Your check digit function is over complicated for checking valid zip codes and also doesn't catch all incorrect zip codes ex. 111111. All you are looking for is that the zip code is five digits long, so something like this:
1 2 3 4
bool isCorrectZipCode(int zipCode) {
int ans = zipCode / pow(10, 5);
return ans > 0 && ans < 10;
}
I don't see anything else wrong with the code other than the goto statement. Not only is it a hackish and ugly way of implementing a loop, but it is also placed in a position that will make the program go into an infinite loop if the user gives the program an incorrect zip code from the command line.