loading
open in
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 Problem Summary: Convert a 5 digit zip code into a postal barcode using the following format
 frame-bar, digit1, digit2, digit3, digit4, digit5, Check digit, frame-bar
 

 
 ///////////
 Algorithm
1. Get 5 digit zip code.
2. Print frame bars
3. Write a barCode function that converts a digit into a barcode.
  a. Print out the zipcode's bar code
4. Write a function that takes the sum of all zip code digits
5. Write a digitCheck function that calculates the Check-digit based on the sum of the zip code digits
  a. Compute Check-digit by adding sum of all zip code digits and adding an integer to make the sum a multiple of 10
 */

//////////////////////////////////////////


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;



//Function prototype
void barCode(int digit);
int zipTotal (int zip);
void digitCheck (int number);



//Main Function
int main() {
    
    //Description Header
    cout << "================================================" << endl;
    cout << "Description: ZipCode to Barcode Program" << endl;
    cout << "Author: Christian Araya" << endl;
    cout << "================================================\n\n\n" << endl;
    
    
    
    //Declarations & Initializations
    int zip = 0;
    int d5 = 0;
    int d4 = 0;
    int d3 = 0;
    int d2 = 0;
    int d1 = 0;
    int zipSum = 0;
    
    
    
    //Input
    cout << "Please enter a 5 digit Zip Code (for example 95014): ";
    cin >>  zip;
    zipSum = zip;
    d5 = zip % 10;
    zip /= 10;
    d4 = zip % 10;
    zip /= 10;
    d3 = zip % 10;
    zip /= 10;
    d2 = zip % 10;
    zip /= 10;
    d1 = zip;
    
    
    cout << "The postal barcode for " << zipSum << " is: \n"  << endl;
    cout << "|";
    barCode(d1);
    barCode(d2);
    barCode(d3);
    barCode(d4);
    barCode(d5);
    digitCheck(zipTotal(zipSum));
    cout << "|\n\n\n";

    return 0;
}



//Function Definitions
void barCode(int digit)
{
    switch (digit)
    {
        case 0:
            cout << "||:::";
            break;
        case 1:
            cout << ":::||";
            break;
        case 2:
            cout << "::|:|";
            break;
        case 3:
            cout << "::||:";
            break;
        case 4:
            cout << ":|::|";
            break;
        case 5:
            cout << ":|:|:";
            break;
        case 6:
            cout << ":||::";
            break;
        case 7:
            cout << "|:::|";
            break;
        case 8:
            cout << "|::|:";
            break;
        case 9:
            cout << "|:|::";
            break;
        default:
            cout << "Please enter valid digits";
    }
}

int zipTotal(int zip)
{
    int sum = 0;
    do
    {
    sum = sum + zip % 10;
    zip = zip / 10;
    }
    while (zip > 0);
    return sum;
}

void digitCheck(int number)
{
    number %= 10;
    int diffNum = 0;
    if ( 0 <= number & number <= 10)
        diffNum = 10 - number;
    
    barCode(diffNum);
}

gcc version 4.6.3