Question
Use C++ please Universal product codes (used in barcodes) include a check digit for error detection. UPC-A is the standard retail price code barcode used
Use C++ please
Universal product codes (used in barcodes) include a check digit for error detection. UPC-A is the standard retail "price code" barcode used in the U.S. A UPC-A barcode contains 12 digits with the rightmost digit computed from the other digits. Label the 12 digits from left to right as
d11d10d9d8d7d6d5d4d3d2d1d0 The check digit d0 is computed as follows
Add the digits in the odd-numbered positions together and multiply by 3.
Add the digits in the even-numbered positions (excluding digit d0) to the sum.
Take the remainder of the sum divided by 10 (i.e. the modulo 10 operation). If the remainder is 0 then the check digit is 0, otherwise subtract the remainder from 10 to derive the check digit.
The code is 12 digits which exceeds what can be stored in a 32-bit integer, so you will need to declare the code as an int64_t (a.k.a. a long long int). if (logical expression yielding a Boolean result) { // Statement(s) performed if the expression is true } else { // Statement(s) performed if the expression is false }
The else clause is optional; see If statement variants
Extracting the digits
Inside the my_code() function read the code from the in stringstream and store it in an int64_t variable. Write the code variable to the out stringstream. Then extract each digit and store them in int32_t variables d0 to d11. Write each digit variable to the out stringstream beginning with d0 and separate the digits by writing a blank (' ') character before each digit. Running the rest of the algorithm is pointless if the digits are not extracted correctly.
Validating the check digit
Write either the string " valid " (if the code's check digit is correct) or " invalid " (if the code's check digit is incorrect) to the out stringstream. " " is an example of an "escape" character denoting a newline. Hence for the UPC-code 123456789012 the output would be:
123456789012 2 1 0 9 8 7 6 5 4 3 2 1 valid
One of the advantages of the code structure introduced in Exercise 01.2 is that we can apply an arbitrary number of tests to your code without my_code() having any knowledge about what tests will be used or how many. Look at asst04_test.cpp to see the tests applied to your code. The unit tests expect the precise format shown above, including the newline. Spaces are not allowed at the beginning or end of the line.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started