Program Specifications 1. Write a program that asks the user for a zip code and
ID: 3626465 • Letter: P
Question
Program Specifications
1. Write a program that asks the user for a zip code and prints the POSTNET A bar code to the console.
Be sure to include the frame bars and check digit in the bar code displayed.
2. Use a vertical bar '|' for full bars and a comma ',' for half bars. For example, 95003 becomes:
||,|,,,|,|,||,,,||,,,,,||,,,||,|
3. Within your program, define the two functions described by the comments and prototypes shown below:
/**
Makes the entire POSTNET "A" bar code for a ZIP code.
@param zip the code to convert to a barcode.
@return a bar code of the zip using "|" as the long
bar and "," as the half bar.
*/
string buildBarcode(int zip);
/**
Turns a single digit into a bar code.
@param digit the single integer digit to encode.
@return a bar code of the digit using "|" as the long
bar and "," as the half bar.
*/
string buildDigit(int digit);
You may change the parameters, but do NOT change the functions names or return types.
4. The program must define between three and seven functions, including main(), and call all the defined functions at least one time.
5. Structure your code such that you declare function prototypes before main() and function definitions aftermain() for all your functions.
6. Remember to include one file comment block, a function comment block for all function prototypes, and follow all the other style rules we have covered so far.
8. Your program must operate like this:
Enter a zip code and I will generate a bar code.
Enter 0 to exit.
Enter a zip code: 95003
The bar code for 95003 is: ||,|,,,|,|,||,,,||,,,,,||,,,||,|
Enter a zip code: 90210
The bar code for 90210 is: ||,|,,||,,,,,|,|,,,||||,,,|,,|,|
Enter a zip code: 62345
The bar code for 62345 is: |,||,,,,|,|,,||,,|,,|,|,|,||,,,|
Enter a zip code: 0
May your mail zip to its destination!
Notice the following:
a. No extra inputs other than entering a zip code
b. Use of 0 as a sentinel to exit the program
9. Remember to follow all the style rules
Function comments
Avoid duplicating code
Indentation in functions and placement of curly braces
No magic numbers
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
string buildDigit(int digit)
{
switch(digit)
{
case 0:return "||,,,";
case 1:return ",,,|,";
case 2:return ",,|,,";
case 3:return ",,||,";
case 4:return ",|,,,";
case 5:return ",|,|,";
case 6:return ",||,,";
case 7:return "|,,,,";
case 8:return "|,,|,";
case 9:return "|,|,,";
}
}
string buildBarcode(int zip)
{
string bar="|";
int sum=0;
int k=10000;
while(zip)
{
bar = bar+buildDigit(zip/k);
sum = sum+zip/k;
zip =zip%k;
k=k/10;
}
bar=bar+buildDigit(10-sum%10);
bar = bar+"|";
return bar;
}
int main()
{
int zip;
cout << "Enter a zip code and I will generate a bar code."<<endl;
cout << "Enter 0 to exit."<<endl;
cout<< "Enter a zip code: ";
cin>>zip;
while(zip!=0)
{
cout<<"The bar code for "<<zip <<" is: "<< buildBarcode(zip)<<endl;
cout<< "Enter a zip code: ";
cin>>zip;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.