# A7-Postal Bar Codes

## Overview

Complete the following for full credit on the assignment:

1. Lesson Exercises

Make sure you have completed the exercises from lesson 7.

2. Review Lab Exercises:

Complete the Review Exercises in CodeLab 7. These exercises will help prepare you for the problem-solving program and should be completed first.

3. Problem Solving Program:

Complete the programming projects following the Program Specifications listed below.

4. Tutorial Lab Exercises:

Complete the Tutorial Exercises in CodeLab 7 before the specified due date. Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first attempt by clicking the "Solution" tab.

5. Turn in Files

Submit your files to Blackboard as explained in the section of this document: What to Turn In.

## Background Information for Program

To sort letters more quickly, the United States Postal Service encourages companies to add a bar code to large volume mailings. One such bar code format is POSTNET "A" which encodes the five digit zip code of the address. The following is an example of a POSTNET A five-digit bar code (from Wikipedia).

POSTNET A bar codes are made up of long and short bars as shown in the diagram below. The diagram also shows the psuedo-binary encoding scheme you can use to decode bars. For example, the number 10100 is 1 x 7 + 0 x 4 + 1 x 2 + 0 x 1 + 0 x 0 = 9. The only exception to the coding scheme is 0, which is encoded as 11 according to the formula.

The layout of a five-digit zip code is shown below. Each side has a full-height frame bar. Inside the frame bars are the five encoded digits followed by a check digit. The check digit is computed by adding up all five digits of the zip code. The check digit is the smallest number you need to add to the sum to make the entire code a multiple of 10. For example, the code 91801 adds up to 19, so the check digit is 1 to make the sum equal to 20.

## Program Specifications

Note that you must complete this program either by yourself or by working with one other student of this class following the rules of Pair Programming for Homework Assignments.

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 (Cabrillo's zip code) 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. Your 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 after `main()` 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.
7. The name of the source code file must be `postal.cpp` and all your code must be in this file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name.

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:
1. No extra inputs other than entering a zip code
2. Use of 0 as a sentinel to exit the program

9. Remember to follow all the style rules from the last assignments, as well as the new rules we covered, including:
1. Function comments (See: Function Comment Block) new!
2. Avoid duplicating code (see textbook page 192)
3. Indentation in functions and placement of curly braces (See: Indentation)
4. No magic numbers (See: No Magic Numbers)
10. Submit your files to Blackboard as explained in the section of this document: What to Turn In.

#### Hints

1. Remember that we write functions for two reasons:
1. To organize our code into modules
2. To reduce repeated code

Thus, you do not want to write 10 different functions to create 10 different bar codes. All these functions work the same way. Instead, you want to write one function to create a bar code from a digit and call the function as many times as needed. For example:

```/**
Turns a single digit into a bar code.

@param digit the single digit to encode.
@return a bar code of the digit using "|" as the long
bar and "," as the half bar.
*/
string buildDigit(int digit);
```
2. Once you extract and encode a digit as a string, you need to arrange the barcode digits in the correct order. One way is to build a barcode one digit at a time, appending each new digit to the front or back of the barcode string. Appending to the front lets you reverse the order. For more information on appending strings, see lesson 3.2.4: Joining Strings (Concatenation).

## Extra Credit

The following are worth extra credit points:

1. Complete the assignment using pair programming. (1 point)
2. Add another function to your `postal.cpp` file that gets a valid zip code from the user: (2 points)
1. The name of your function must be either `read_valid_zip()` or `readValidZip()`
2. Verify that the zip code entered is a positive number
3. Verify that the zip code entered is no more than 5 digits in length
4. Recover from non-numeric input as needed
5. 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: abc
You must enter digits, not words
Enter a zip code: 123456
ZIP codes must be <= 99999
Enter a zip code: -1
ZIP codes must be > 0
Enter a zip code: 95003
The bar code for 95003 is: ||,|,,,|,|,||,,,||,,,,,||,,,||,|

Enter a zip code: 0

May your mail zip to its destination!
```
Your program must call the this extra function for credit.

Make certain that your `README.txt` file describes any extra credit attempted.

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus page.

#### Lesson Exercises

• 2: All lesson exercises attempted and turned in
• 1: Some lesson exercises completed and turned in
• 0: No lesson exercises completed or turned in

#### Program Compilation

• 4: Source code compiles with no errors or warnings
• 3: Source code compiles with 1 warning
• 2: Source code compiles with 2 warnings
• 1: Source code compiles with 3+ warnings
• 0: Does not compile or wrong file turned in

#### Functionality

• 10: Demonstrates mastery of the assignment
• Applies concepts from the lessons appropriately
• Meets all specifications (see above) with particularly elegant solutions
• Runs to completion with no abnormal error conditions
• Generates correct output given correct input
• Behaves in a reasonable way in response to incorrect data
• 8: Has all the functionality expected of the assignment
• Demonstrates many techniques from the lesson
• Attempts to meet all specifications (see above)
• Implementation seems more complicated than necessary.
• May have one minor error
• 6: Has most of the functionality expected of the assignment
• Demonstrates some techniques from the lesson
• Attempts to meet all but one of the specifications (see above)
• Implementation seems excessively complicated.
• May have 2-3 minor errors
• 4: Has some of the functionality expected of the assignment
• Demonstrates some techniques from the lesson
• Attempts to meet at least 1/2 of the specifications (see above)
• Implementation seems excessively complicated.
• May have more than 3 minor errors
• 2: Serious functional problems but shows some effort and understanding
• Attempts to meet less than 1/2 of the of the specifications (see above)
• Has a major error or many minor errors
• Implementation seems very convoluted
• Demonstrates few techniques from the lesson
• 0: Does not execute

#### Program Style

• 4: Code is well-documented including:
• Name, date, and program description in file comment block
• Follows specified format for file comment block
• Has a function comment block for all function declarations following the specified format
• Proper use of spaces around operators
• No tab characters are present in the source code
• As described in How To Document and Organize C++ Code
• Correct file name used
• 3: Code has a minor documentation error
• 2: Code has some documentation errors
• 1: Code has many documentation errors
• 0: No apparent attempt to follow documentation standards or write documentation comments

#### CodeLab Exercises

Number completed correctly / number exercises * 8 and rounded up to the nearest integer.

#### `README.txt` File

• 2: `README.txt` file submitted following the instructions
• 1: `README.txt` file submitted but some information was missing
• 0: No `README.txt` file submitted

Total possible: 30, plus extra credit

## What to Turn In

Submit your assignment to Blackboard, in the assignment folder that matches the name of this assignment, following the instructions for submitting homework. Include the following items for grading:

1. `README.txt` file
2. All the exercise files from Lesson 7
3. `postal.cpp`

Your assignment files must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline.

Home | Blackboard | Day Schedule | Eve Schedule
Syllabus | Help | FAQ's | HowTo's | Links
Last Updated: April 01 2012 @22:18:54