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