Function

I'm new to coding. I'm working on a class assignment that converts a zip code to a barcode. I'm stuck trying to figure out how simplify my code by calling a barcode function 6 times. For example,

barcode (d1);
barcode (d2);
barcode (d3);
barcode (d4);
barcode (d5);
barcode (result);

With a barcode that should look like this:

void barcode(int d)

{

switch (d)

{

case 4:

cout << ":|::|";

break;

default:

break;

}

}
//Example ends

Below is the code I have written that needs to be simplify. I'll appreciate any feedback.

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <cmath>

using namespace std;
   
void dsp (int d1);
void dsp (int d2);
void dsp (int d3);
void dsp (int d4);
void dsp (int d5);
void dsp (int result);
int main()

{

int zipcode = int();
int d1 = int();
int d2 = int();
int d3 = int ();
int d4 = int ();
int d5 = int();
int digitsum = int();
int result = int();

    //User input
    cout << "Please enter a zip code: ";
    cin >> zipcode;
    
    //Seperating digits
    d5 = zipcode % 10;
    zipcode /=10;
    d4 = zipcode % 10;
    zipcode /=10;
    d3 = zipcode % 10;
    zipcode /=10;
    d2 = zipcode % 10;
    zipcode /=10;
    d1 = zipcode % 10;
    zipcode /=10;
    
    //Addition of digits
    digitsum = d1 + d2 + d3 + d4 + d5;
    
    
    //Correction digit
    result = 10 - (digitsum%10);
    cout << "Correction " << result << endl;

//First digit barcode
    void dsp(int d1);
        switch (d1)
    {
        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;
        case 0:
            cout << "||:::";
            break;
        default:
            cout << "";
    }
    

//Second digit barcode
    void dsp(int d2);
        switch (d2)
    {
        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;
        case 0:
            cout << "||:::";
            break;
        default:
            cout << "";
    }
    
    //Third digit barcode
    void dsp(int d3);
        switch (d3)
    {
        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;
        case 0:
            cout << "||:::";
            break;
        default:
            cout << "";
    }
    
    //Fourth digit barcode
    void dsp(int d4);
        switch (d4)
    
    {
        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;
        case 0:
            cout << "||:::";
            break;
        default:
            cout << "";
    }
    
    //Fifth digit barcode
    void dsp(int d5);
        switch (d5)
    
    {
      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;
    case 0:
        cout << "||:::";
            break;
    default:
        cout << "";
    }
            
    //Correction digit barcode
    void dsp(int result);
        switch (result)
        {
            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;
            case 0:
                cout << "||:::";
                break;
            default:
                cout << "";
        }
return 0;
 }
int zipcode = int();
A simpler way to initialise would be: int zipcode{};

1
2
3
4
5
6
void dsp (int d1);
void dsp (int d2);
void dsp (int d3);
void dsp (int d4);
void dsp (int d5);
void dsp (int result);

All these functions have the same signature. i.e. they all take an int and no return value. You're also declaring the same functions in lines 50, 89 127, 166, 205 and 244, rather than defining them. You're also trying to define a function in a function, which isn't allowed in C++. It's a fluke that your code compiles.

It might be easier if you store your zipcode in a string, so that way you can access each digit easily.
If you don't want to use strings, I'd recommend to use a vector or an array to store each digit , rather than declaring five variables.
Either of these methods allow you to loop through each element and call dsp() on them.
Hello fout,

Unless you come up with some kind of loop there is no shorter way of calling "dsp" 6 different times.

Although you have not defined the "dsp" function yet. You have declared the function type 6 times after main.void dsp(int d5);. The ';' makes this a forward declaration which if better put before main. You need to replace the ';' with an opening '{' and put a closing '}' after the closing '}' of the switch statement. As it is your code did not compile for me.

You only need to define one function of "dsp" then in main call it with the different parameters like:
1
2
3
4
5
6
dsp(d1);
dsp(d2);
dsp(d3);
dsp(d4);
dsp(d5);
dsp(result);


The program works. It just needs the right changes to use it correctly.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.