cperryoh

Untitled

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