-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckISBN10.cpp
44 lines (39 loc) · 862 Bytes
/
checkISBN10.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
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
bool checkISBN(int *arr) {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += ((10 - i) * arr[i]) % 11;
}
if (sum % 11 == 0) return true;
return false;
}
int main() {
string isbn;
cout << "Enter ISBN-10 digits: ";
cin >> isbn;
int arr[10];
for (int i = 0; i < 10; i++) {
if (isbn[i] == 'X') {
if (i == 9) {
arr[i] = 10;
} else {
cout << "'X' is only allowed as the last character" << endl;
return 1;
}
} else if (isdigit(isbn[i])) {
arr[i] = isbn[i] - '0';
} else {
cout << "Enter valid characters" << endl;
}
}
bool match = checkISBN(arr);
if (match)
cout << "ISBN-10 is correct" << endl;
else
cout << "ISBN-10 is not correct" << endl;
return 0;
}