Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // UPC code checker.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- int getCheckDigit(long long int upc, int firstDigit);
- int getDigit(int numOfPlaces, int placeIndex, long long int number);
- int main()
- {
- while (true) {
- std::cout << "Do you have a code to test [y/n]? ";
- std::string test = "";
- std::cin >> test;
- if (test=="y") {
- long long int upcCode;
- int firstDigit;
- //get upc code
- std::cout << "Please enter a upc code: ";
- std::cin >> upcCode;
- //get first digit
- std::cout << "Enter the first digit: ";
- std::cin >> firstDigit;
- //remove checkdigit
- long long int codeWithoutCheckDigit = upcCode / 10;
- //get what check digit is supposed to be
- int checkDigit = getCheckDigit(codeWithoutCheckDigit, firstDigit);
- //compare calculated check digit to actual check digit and determain if the code is valid or not
- if (checkDigit == getDigit(12, 12, upcCode)) {
- std::cout << "The code is valid\n\n";
- }
- else {
- std::cout << "The code is invalid\n\n";
- }
- }
- else if (test=="n") {
- std::cout << ("Exiting program");
- //quit
- return 0;
- }
- else {
- std::cout << "That is not valid please try again \n\n";
- }
- }
- }
- //gets the check digit from a upc code
- int getCheckDigit(long long int upc,int firstDigit) {
- //odd and even digit sums
- int oddDigits=0, evenDigits=0;
- //determains how many digits are in the code based off the first digit
- int digitCount = firstDigit == 0 ? digitCount = 10 : digitCount = 11;
- //sums all the odd and even digits
- for (int i = 1; i <= digitCount; i++) {
- //gets digit from code
- int digit = getDigit(digitCount, i, upc);
- //if the first digit is zero then make the odds even and evens odd because there is one less digit at the begining
- if (firstDigit == 0) {
- if (i % 2 == 0) {
- oddDigits += digit;
- }
- else {
- evenDigits += digit;
- }
- }
- //if the first digit is not zero don't do anything and parse evens and odds normally
- else {
- if (i % 2 == 0) {
- evenDigits += digit;
- }
- else {
- oddDigits += digit;
- }
- }
- }
- //math to get check digit
- oddDigits *= 3;
- evenDigits += oddDigits;
- evenDigits %= 10;
- //return check digit
- return 10 - evenDigits;
- }
- //parses int
- int getDigit(int numOfPlaces, int placeIndex, long long int number) {
- //make a temp var for number as it is being parsed
- long long int temp = number;
- //make a counter for the loop to keep track of which place the loop is at
- int counter = 0;
- /*
- * For loop that loops via powers of 10 downwards starting at 10^(numOfPlaces-1)
- * Ex: 100->10->1
- * if the counter is equal to the requested digit stop the loop and return
- * loop will eventually stop when i == 1
- */
- for (long long int i = pow(10, numOfPlaces - 1); i != 1; i /= 10) {
- //get digit
- long long int digit = temp / i;
- //removes last digit
- temp %= i;
- //check if this is the required digit
- if (counter == (placeIndex - 1)) {
- return digit;
- }
- //if the loop did not return, increment counter and move onto next digit
- counter++;
- }
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement