Hello! I would like to know how to write one function for the output of the digits below. Would you be able to advise?
Instead of copying and pasting each switch case portion for each digit output. What is an alternate way?
#include <iostream>
usingnamespace std;
int main()
{
int zipcode = int();
int digit1 = int();
int digit2 = int();
int digit3 = int();
int digit4 = int();
int digit5 = int();
int digitsum = int();
int diff = int();
int cordigit = int();
char one = '|';
char zero = ':';
cout << "Enter your zipcode: ";
cin >> zipcode;
digit1 = (zipcode % 100000) / 10000;
cout << "Digit 1: " << digit1 << endl;
digit2 = (zipcode % 10000) / 1000;
cout << "Digit 2: " << digit2 << endl;
digit3 = (zipcode % 1000) / 100;
cout << "Digit 3: " << digit3 << endl;
digit4 = (zipcode % 100) / 10;
cout << "Digit 4: " << digit4 << endl;
digit5 = zipcode % 10;
cout << "Digit 5: " << digit5 << endl;
digitsum = digit1 + digit2 + digit3 + digit4 + digit5;
cout << "Sum of Digits: " << digitsum << endl;
diff = digitsum % 10;
//cout << "difference" << diff << endl;
if (diff < 5)
{
cordigit = (5 - diff);
cout << "Correction Digit: " << cordigit << endl;
}
if (diff == 5)
{
cordigit = (5 + diff);
cout << "Correction Digit: " << cordigit << endl;
}
if (diff > 5)
{
cordigit = (10 - diff);
cout << "Correction Digit: " << cordigit << endl;
}
switch (digit1)
{
case 1:
cout << "Zipcode Barcode is |" << zero << zero << zero << one << one;
break;
case 2:
cout << "Zipcode Barcode is |" << zero << zero << one << zero << one;
break;
case 3:
cout << "Zipcode Barcode is |" << zero << zero << one << one << zero;
break;
case 4:
cout << "Zipcode Barcode is |" << zero << one << zero << zero << one;
break;
case 5:
cout << "Zipcode Barcode is |" << zero << one << zero << one << zero;
break;
case 6:
cout << "Zipcode Barcode is |" << zero << one << one << zero << zero;
break;
case 7:
cout << "Zipcode Barcode is |" << one << zero << zero << zero << one;
break;
case 8:
cout << "Zipcode Barcode is |" << one << zero << zero << one << zero;
break;
case 9:
cout << "Zipcode Barcode is |" << one<< zero << one << zero << zero;
break;
case 0:
cout << "Zipcode Barcode is |" << one << one << zero << zero << zero;
break;
}
switch (digit2)
{
case 1:
cout << zero << zero << zero << one << one;
break;
case 2:
cout << zero << zero << one << zero << one;
break;
case 3:
cout << zero << zero << one << one << zero;
break;
case 4:
cout << zero << one << zero << zero << one;
break;
case 5:
cout << zero << one << zero << one << zero;
break;
case 6:
cout << zero << one << one << zero << zero;
break;
case 7:
cout << one << zero << zero << zero << one;
break;
case 8:
cout << one << zero << zero << one << zero;
break;
case 9:
cout << one << zero << one << zero << zero;
break;
case 0:
cout << one << one << zero << zero << zero;
break;
}
switch (digit3)
{
case 1:
cout << zero << zero << zero << one << one;
break;
case 2:
cout << zero << zero << one << zero << one;
break;
case 3:
cout << zero << zero << one << one << zero;
break;
case 4:
cout << zero << one << zero << zero << one;
break;
case 5:
cout << zero << one << zero << one << zero;
break;
case 6:
cout << zero << one << one << zero << zero;
break;
case 7:
cout << one << zero << zero << zero << one;
break;
case 8:
cout << one << zero << zero << one << zero;
break;
case 9:
cout << one << zero << one << zero << zero;
break;
case 0:
cout << one << one << zero << zero << zero;
break;
}
switch (digit4)
{
case 1:
cout << zero << zero << zero << one << one;
break;
case 2:
cout << zero << zero << one << zero << one;
break;
case 3:
cout << zero << zero << one << one << zero;
break;
case 4:
cout << zero << one << zero << zero << one;
break;
case 5:
cout << zero << one << zero << one << zero;
break;
case 6:
cout << zero << one << one << zero << zero;
break;
case 7:
cout << one << zero << zero << zero << one;
break;
case 8:
cout << one << zero << zero << one << zero;
break;
case 9:
cout << one << zero << one << zero << zero;
break;
case 0:
cout << one << one << zero << zero << zero;
break;
}
switch (digit5)
{
case 1:
cout << zero << zero << zero << one << one;
break;
case 2:
cout << zero << zero << one << zero << one;
break;
case 3:
cout << zero << zero << one << one << zero;
break;
case 4:
cout << zero << one << zero << zero << one;
break;
case 5:
cout << zero << one << zero << one << zero;
break;
case 6:
cout << zero << one << one << zero << zero;
break;
case 7:
cout << one << zero << zero << zero << one;
break;
case 8:
cout << one << zero << zero << one << zero;
break;
case 9:
cout << one << zero << one << zero << zero;
break;
case 0:
cout << one << one << zero << zero << zero;
break;
}
switch (cordigit)
{
case 1:
cout << zero << zero << zero << one << one;
break;
case 2:
cout << zero << zero << one << zero << one;
break;
case 3:
cout << zero << zero << one << one << zero;
break;
case 4:
cout << zero << one << zero << zero << one;
break;
case 5:
cout << zero << one << zero << one << zero;
break;
case 6:
cout << zero << one << one << zero << zero;
break;
case 7:
cout << one << zero << zero << zero << one;
break;
case 8:
cout << one << zero << zero << one << zero;
break;
case 9:
cout << one << zero << one << zero << zero;
break;
case 0:
cout << one << one << zero << zero << zero;
break;
}
cout << "|" << endl;
return 0;
}
Whoa dude! Arrays! They are useful.
A single function to print a barcode for a 5 digit number?
I think we can squeeze it down a bit.
edit: I'm sorry. I just noticed it needs to work for a single digit too.
This is better: