The Universal Product Code (UPC) on most grocery items contains a zero that is printedto the left of the bar code and 10 digits that are printed in two groups of five each belowthe code. The first five digits represent a manufacturer and the second five digits representa specific product. To the right of the bar code, a check digit is printed. Write aprogram that allows the user to enter the five-digit manufacturer code, the five-digitproduct code, and the check digit. Display the result of a Boolean comparison (0 or 1)that shows whether the check digit is correct according to the following algorithm.(Hint: Use a combination of modulus, multiplication, and subtraction to separate thedigits in the entered code.)a. Sum the second and fourth numbers in the manufacturer code.b. Sum the first, third, and fifth numbers in the product code.c. Sum the results of step a and step b, and multiply the total by 3.d. Sum the first, third, and fifth numbers in the manufacturer code.e. Sum the second and fourth digits in the product code.f. Sum the results of steps d and e and add to the result from step c.g. Take the remainder when the result of step f is divided by 10.h. Subtract the result of step g from 10. Compare this result to the check digit entered by theuser. If the result and check digit are equal (if the Boolean value of the comparison is 1),then the UPC is a valid one.Your solution should prompt the user for the UPC numbers.Your solution should report to the user if the UPC code in valid or not.The user should be able to enter multiple tests.The program must be programmed in a simple c++ without the use of arraysThe Algorithm:In the UPC-A system, the check digit is calculated as follows:1. Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.2. Add the digits in the even-numbered positions (second, fourth, sixth, etc.) to the result.3. Find the result modulo 10 (i.e. the remainder when divided by 10.. 10 goes into 58 5 times with 8 leftover).4. If the result is not zero, subtract the result from ten.For example, in a UPC-A barcode "05150024163x" where x is the unknown check digit, x can be calculated byadding the odd-numbered digits (0 + 1 + 0 + 0 + 2 + 1 + 3 = 7),multiplying by three (7 × 3 = 21),adding the even-numbered digits (21 + (5 + 5 + 0 + 4 + 6) = 41),calculating modulo ten (41 mod 10 = 1),subtracting from ten (10 − 1 = 9).The check digit is thus 9.Do you have a number to test? (y or n): y Enter the very first number of the UPC. 0 Enter the very last number of the UPC. 9 Enter your Manufacturer number (the first set of 5 digits) 51500 Enter your Product number (the second set of 5 digits) 24163 UPC code 051500241639 is valid. Do you have a number to test? (y or n): y Enter the very first number of the UPC. 8 Enter the very last number of the UPC. 9 Enter your Manufacturer number (the first set of 5 digits) 76045 Enter your Product number (the second set of 5 digits) 00414 UPC code 876045004149 is valid. Do you have a number to test? (y or n) y Enter the very first number of the UPC. 8 Enter the very last number of the UPC. 9 Enter your Manufacturer number (the first set of 5 digits) 76055 Enter your Product number (the second set of 5 digits) 00414 Invalid UPC code. Do you have a number to test? Cy or n): n Type any key and then hit to quit the program.

Respuesta :

Answer:

Check the explanation

Explanation:

#include <iostream>

#include <string>

using namespace std;

int main()

{

int last;

string first, manuCode, prodCode;

string ans = "y";

bool done = false;

while(!done)

{

cout << "Do you have a number to test? y or n : ";

cin >> ans;

if(ans != "y" && ans != "Y")

done = true;

else

{

cout << "Enter the first number of the UPC: ";

cin >> first;

cout << "Ebter the last number of the UPC: ";

cin >> last;

cout << "Etner the Manufacterer number (first set of 5 digits): ";

cin >> manuCode;

cout << "Enter the Product number (second set of 5 digits): ";

cin >>prodCode;

 

int sodd = 0, seven = 0;

string s = first + manuCode + prodCode;

int rem, check;

 

for(int i = 1; i <= s.length(); i++)

{

if(i % 2 == 1) // odd

sodd += s[i-1] - '0'; //subtract '0' to get numeric value from ascii character

else

seven += s[i-1] - '0';

}

 

rem = (3 * sodd + seven) % 10;

check = 10 - rem;

 

s += to_string(last);

 

if(check == last)

cout << "UPC code " << s << " is valid." << endl<< endl;

else

cout << "Invalid UPC code" << endl << endl;

}

}

}

output

-=====

Do you have a number to test? y or n :y

Enter the first number of the UPC: 0

Ebter the last number of the UPC: 9

Etner the Manufacterer number (first set of 5 digits): 51500

Enter the Product number (second set of 5 digits): 24163

UPC code 051500241639 is valid.

Do you have a number to test? y or n : y

Enter the first number of the UPC: 8

Ebter the last number of the UPC: 9

Etner the Manufacterer number (first set of 5 digits): 76045

Enter the Product number (second set of 5 digits): 00414

UPC code 876045004149 is valid.

Do you have a number to test? y or n : y

Enter the first number of the UPC: 8

Ebter the last number of the UPC: 9

Etner the Manufacterer number (first set of 5 digits): 76055

Enter the Product number (second set of 5 digits): 00414

Invalid UPC code

Do you have a number to test? y or n : n