Advertisement
cperryoh

UPC code parser

Feb 20th, 2021 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. // UPC code checker.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5.  
  6. int getCheckDigit(long long int upc, int firstDigit);
  7. int getDigit(int numOfPlaces, int placeIndex, long long int number);
  8. int main()
  9. {
  10.     while (true) {
  11.         std::cout << "Do you have a code to test [y/n]? ";
  12.         std::string test = "";
  13.         std::cin >> test;
  14.         if (test=="y") {
  15.             long long int upcCode;
  16.             int firstDigit;
  17.  
  18.             //get upc code
  19.             std::cout << "Please enter a upc code: ";
  20.             std::cin >> upcCode;
  21.  
  22.             //get first digit
  23.             std::cout << "Enter the first digit: ";
  24.             std::cin >> firstDigit;
  25.  
  26.             //remove checkdigit
  27.             long long int codeWithoutCheckDigit = upcCode / 10;
  28.  
  29.  
  30.             //get what check digit is supposed to be
  31.             int checkDigit = getCheckDigit(codeWithoutCheckDigit, firstDigit);
  32.  
  33.             //compare calculated check digit to actual check digit and determain if the code is valid or not
  34.             if (checkDigit == getDigit(12, 12, upcCode)) {
  35.                 std::cout << "The code is valid\n\n";
  36.             }
  37.             else {
  38.                 std::cout << "The code is invalid\n\n";
  39.             }
  40.         }
  41.         else if (test=="n") {
  42.             std::cout << ("Exiting program");
  43.  
  44.             //quit
  45.             return 0;
  46.         }
  47.         else {
  48.             std::cout << "That is not valid please try again \n\n";
  49.         }
  50.     }
  51. }
  52.  
  53. //gets the check digit from a upc code
  54. int getCheckDigit(long long int upc,int firstDigit) {
  55.     //odd and even digit sums
  56.     int oddDigits=0, evenDigits=0;
  57.  
  58.     //determains how many digits are in the code based off the first digit
  59.     int digitCount = firstDigit == 0 ? digitCount = 10 : digitCount = 11;
  60.  
  61.     //sums all the odd and even digits
  62.     for (int i = 1; i <= digitCount; i++) {
  63.  
  64.         //gets digit from code
  65.         int digit = getDigit(digitCount, i, upc);
  66.  
  67.         //if the first digit is zero then make the odds even and evens odd because there is one less digit at the begining
  68.         if (firstDigit == 0) {
  69.             if (i % 2 == 0) {
  70.                 oddDigits += digit;
  71.             }
  72.             else {
  73.                 evenDigits += digit;
  74.             }
  75.         }
  76.  
  77.         //if the first digit is not zero don't do anything and parse evens and odds normally
  78.         else {
  79.             if (i % 2 == 0) {
  80.                 evenDigits += digit;
  81.             }
  82.             else {
  83.                 oddDigits += digit;
  84.             }
  85.         }
  86.     }
  87.     //math to get check digit
  88.     oddDigits *= 3;
  89.     evenDigits += oddDigits;
  90.     evenDigits %= 10;
  91.  
  92.     //return check digit
  93.     return 10 - evenDigits;
  94. }
  95.  
  96. //parses int
  97. int getDigit(int numOfPlaces, int placeIndex, long long int number) {
  98.     //make a temp var for number as it is being parsed
  99.     long long int temp = number;
  100.  
  101.     //make a counter for the loop to keep track of which place the loop is at
  102.     int counter = 0;
  103.  
  104.     /*
  105.     * For loop that loops via powers of 10 downwards starting at 10^(numOfPlaces-1)
  106.     * Ex: 100->10->1
  107.     * if the counter is equal to the requested digit stop the loop and return
  108.     * loop will eventually stop when i == 1
  109.     */
  110.     for (long long int i = pow(10, numOfPlaces - 1); i != 1; i /= 10) {
  111.         //get digit
  112.         long long int digit = temp / i;
  113.  
  114.         //removes last digit
  115.         temp %= i;
  116.  
  117.         //check if this is the required digit
  118.         if (counter == (placeIndex - 1)) {
  119.             return digit;
  120.         }
  121.  
  122.         //if the loop did not return, increment counter and move onto next digit
  123.         counter++;
  124.     }
  125.     return temp;
  126. }
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement