Question: Please help me figure out why my code is not working properly.I had thought my logic was sound but later found it will run one
Please help me figure out why my code is not working properly.I had thought my logic was sound but later found it will run one time through fine however if the user opts to enter another value it will always be returned as an error.
#include
#include
#include
using namespace std;
int const TRUE = 1;
int const FALSE = 0;
// declares functions
void GetZipcode();
void RunAgain();
int GetSum(char d1, char d2, char d3, char d4, char d5);
int GetCheckDigitValue(int sum);
string GetDigitCode(char digit);
int main (){
// calls function
GetZipcode();
return(0);
}
void GetZipcode(){
// declares values
char d1;
char d2;
char d3;
char d4;
char d5;
//gather data at once but reads in each value
cout << "Enter a zipcode: " ;
cin.get(d1);
cin.get(d2);
cin.get(d3);
cin.get(d4);
cin.get(d5);
cin.ignore(200, ' ');
//checks to make sure the data is only digits if not usable prints error message
if (isdigit(d1) && isdigit(d2) && isdigit(d3) && isdigit(d4) && isdigit(d5)){
//prints barcode if zipcode entered is valid
cout <<" | " << GetDigitCode(d1) << GetDigitCode(d2) << GetDigitCode(d3) << GetDigitCode(d4) << GetDigitCode(d5) << GetDigitCode(GetCheckDigitValue(GetSum(d1,d2,d3,d4,d5))+'0') << " | " << endl;
//calls function to ask to run program again
RunAgain();
//prints error message and calls function to ask to run again
} else {
cout << "Error, zipcode must be 5 digits and contain no special characters" << endl;
RunAgain();
}
}
// run program again function
void RunAgain(){
char answer;
cout << " Continue (y/n)?";
cin >> answer;
if ( answer == 'y' || answer == 'Y'){
GetZipcode();
}
}
//gets sum of zip for later use
int GetSum(char d1, char d2, char d3, char d4, char d5){
int sum = 0;
sum = (d1 - '0') + (d2 - '0') + (d3 - '0') + (d4 - '0') + (d5 - '0');
return sum;
}
//uses sum to find check digit value
int GetCheckDigitValue(int sum){
int x = 0;
do {
x++;
} while(((sum + x) % 10 == 0) != 1);
return x;
}
// converts each digit to barcode value
string GetDigitCode(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 "|:|::"; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
