Universal product codes are the familiar bar codes that identify products so tha
ID: 3883029 • Letter: U
Question
Universal product codes are the familiar bar codes that identify products so that they can be automatically priced at the checkout counter. A UPC is a 12-digit code in which the first digit characterizes the type of product (0 identifies an ordinary grocery item, 2 is an item sold by weight, 3 is a medical item, 4 is a special item, 5 is a coupon, and 6 and 7 are items not sold in retail stores). The next 5 digits identify the manufacturer, the next five digits identify the product, and the last digit is a check digit. (All UPC codes have a check digit. It is always present on the bar code, but it may not appear in the printed version.) For example, the UPC for a package of 10 Ortega taco shells is 0-54400-00800-5. The first zero indicates this is an ordinary grocery item, the next five digits 54400 identify the manufacturer Nabisco Foods, and the next five dights 00800 identify the product as a package of 10 Ortega taco shells. The check digit is computed as follows. First compute s, where s is 3 times the sum of every other number starting with the first plus the sum of the remaining numbers, starting with the second. The check digit is the number c, between 0 and 9 satisfying (c+s) mod 10 = 0. For the code on the package of taco shells, we would have ... 0-54400-00800-5 0 + 5 + 4 + 4 + 0 + 0 + 0 + 0 + 8 + 0 + 0 + 5 *3 *1 *3 *1 *3 *1 *3 *1 *3 *1 *3 *1 - - - - - - - - - - - - s = 0 + 5 +12 + 4 + 0 + 0 + 0 + 0 +24 + 0 + 0 + 5 = 50 Since 50 mod 10 = 0, the number has been read correctly. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following product has been scanned and the number is 0-54400-00800-5. See if it is valid. 0 - 5 4 4 0 0 - 0 0 8 0 0 - 5 x x x x x x x x x x x x 3 1 3 1 3 1 3 1 3 1 3 1 - - - - - - - - - - - - 0 5 12 4 0 0 0 0 24 0 0 5 = 50 50 is a multiple of 10, therefore we conclude that the number is ok - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - For this assignment you are to write a C++ program that will read in UPC numbers and check them for correctness. The data includes numbers that are ok, two numbers that are wrong, and a number that is scanned backwards. data to be used for input upc 9-10679-87078-4 upc 7-93190-02004-9 upc 0-41100-33077-2 upc 0-76281-70322-0 upc 0-41270-87455-4 upc 0-41270-88000-8 upc 0-41163-41030-5 upc 0-44300-12392-9 upc 0-36000-28590-1 upc 8-00088-07214-0 sample output upc 9-10679-87078-4 ok upc 7-93190-02004-9 ok upc 0-41100-33077-2 ok upc 0-76281-70322-0 ok upc 0-41270-87455-4 error upc 0-41270-88000-8 ok upc 0-41163-41030-5 ok upc 0-44300-12392-9 error upc 0-36000-28590-1 ok upc 8-00088-07214-0 ok - backwards Some Logic you may want to use: total = 0 multiplier = 3 loop 20 times incrementing i by 1 pick off the i'th character if the character is a digit then convert the character to a number total = total + number*multiplier change the multiplier end loop if the total is a multiple of ten then write 'ok' else do the same check as above but go through the loop backwards if the total is a multiple of ten then write 'ok - backwards' else write 'error'
Explanation / Answer
Given below is the program which takes 1 code at a time and check if its valid. In case you need the program to read from a file, use the 2nd version at end. Hope the answer helps. If it did, please don't forget to rate the answer. Thank you very much.
#include <iostream>
using namespace std;
int main()
{
string UPC;
cout << "Enter UPC number: ";
getline(cin, UPC);
int total = 0;
int multiplier = 3;
char ch;
for(int i = 0; i < 20; i++)
{
ch = UPC[i];
if(ch >= '0' && ch <= '9') //ch is a digit
{
int digit = ch - '0';
total += digit * multiplier;
multiplier = 4 - multiplier; //change 3 to 1 , otherwise 1 to 3
}
}
if(total % 10 == 0) //total is divisble by 10
cout << UPC << " ok" << endl;
else
{
//check backwards
total = 0;
multiplier = 3;
for(int i = 19; i >= 0; i--)
{
ch = UPC[i];
if(ch >= '0' && ch <= '9') //ch is a digit
{
int digit = ch - '0';
total += digit * multiplier;
multiplier = 4 - multiplier; //change 3 to 1 , otherwise 1 to 3
}
}
if(total % 10 == 0) //total is divisble by 10
cout << UPC << " ok backwards" << endl;
else
cout << UPC << " error" << endl;
}
}
output
$ g++ UPC_check.cpp
$ ./a.out
Enter UPC number: upc 0-44300-12392-9
upc 0-44300-12392-9 error
$ ./a.out
Enter UPC number: upc 0-36000-28590-1
upc 0-36000-28590-1 ok
$ ./a.out
Enter UPC number: upc 8-00088-07214-0
upc 8-00088-07214-0 ok backwards
program to read from file:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string UPC;
string filename;
cout << "Enter filename: ";
cin >> filename;
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "could not open input file - " << filename << endl;
return 1;
}
while(true)
{
getline(infile, UPC);
if(infile.eof())
break;
int total = 0;
int multiplier = 3;
char ch;
for(int i = 0; i < 20; i++)
{
ch = UPC[i];
if(ch >= '0' && ch <= '9') //ch is a digit
{
int digit = ch - '0';
total += digit * multiplier;
multiplier = 4 - multiplier; //change 3 to 1 , otherwise 1 to 3
}
}
if(total % 10 == 0) //total is divisble by 10
cout << UPC << " ok" << endl;
else
{
//check backwards
total = 0;
multiplier = 3;
for(int i = 19; i >= 0; i--)
{
ch = UPC[i];
if(ch >= '0' && ch <= '9') //ch is a digit
{
int digit = ch - '0';
total += digit * multiplier;
multiplier = 4 - multiplier; //change 3 to 1 , otherwise 1 to 3
}
}
if(total % 10 == 0) //total is divisble by 10
cout << UPC << " ok backwards" << endl;
else
cout << UPC << " error" << endl;
}
}
infile.close();
}
input file: upc.txt
upc 9-10679-87078-4
upc 7-93190-02004-9
upc 0-41100-33077-2
upc 0-76281-70322-0
upc 0-41270-87455-4
upc 0-41270-88000-8
upc 0-41163-41030-5
upc 0-44300-12392-9
upc 0-36000-28590-1
upc 8-00088-07214-0
output
Enter filename: upc.txt
upc 9-10679-87078-4 ok
upc 7-93190-02004-9 ok
upc 0-41100-33077-2 ok
upc 0-76281-70322-0 ok
upc 0-41270-87455-4 error
upc 0-41270-88000-8 ok
upc 0-41163-41030-5 ok
upc 0-44300-12392-9 error
upc 0-36000-28590-1 ok
upc 8-00088-07214-0 ok backwards
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.