@AmyShackles/

CheckYourDigits

C++11

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using std::cin;
using std::cout;

int productOfDigit(int digit, int count) {
  int product = digit * (10 - count);
  return product;
}

int tripleDigitValue(int digit) {
  int tripleDigit = digit * 3;
  return tripleDigit;
}

int main() {
  char digit;
  int count = 0;
  int checksum10 = 0;
  int checksum13 = 0;
  int position = 1;
  cout << "Enter a number: ";
  digit = cin.get();
  if (digit == '-') {
    digit = cin.get();
  }
  while (digit != 10) {
    if (position % 2 == 0) {
      checksum13 += tripleDigitValue(digit - '0');
    } else {
      checksum13 += (digit - '0');
    }
    checksum10 += productOfDigit((digit - '0'), count);
    count++;
    digit = cin.get();
    if (digit == '-') {
      digit = cin.get();
    }
    position++;
  }
  if (count == 9) {
    int checkdigit = 11 - (checksum10 % 11);
    cout << "\nCheck digit is: " << checkdigit << "\n";
    checksum10 += checkdigit;
  }
  else if (count <= 10) {
    cout << "\nChecksum is " << checksum10 << ". \n";
    if (checksum10 % 11 == 0) {
      cout << "Checksum is divisible by 11.  Valid. \n";
    } else {
      cout << "Checksum is not divisible by 11.  Invalid. \n";
    }
  } else {
    if (count == 12) {
      int checkdigit = 10 - (checksum13 % 10);
      cout << "\nCheck digit is: " << checkdigit << "\n";
      checksum13 += checkdigit;
    }
    else if (checksum13 % 10 == 0) {
      cout << "\nChecksum is divisible by 10.  Valid.\n";
    } else {
      cout << "Checksum is not divisible by 10.  Invalid.\n";
    }
  }
  cout << "\nYou entered " << count << " digits\n";
}