C++ Directions: input ISBN number from user and output “Valid” if the number is
ID: 3728890 • Letter: C
Question
C++
Directions: input ISBN number from user and output “Valid” if the number is valid or “Invalid” otherwise. Terminate the input when the user enters “done”
ISBN
number for a book
10 digit number made up of 4 sections
each section seperated by ‘ - ‘
Example: ISBN number is: 1-214-02031-3
1 represents a country
must be 1 digit
214 represents the publisher
must be 3 digits
02031 reprsents the book number
must be 5 digits
3 is the checksum
must be 1 digit or the letter ‘x’
checksum is a digit to see if the ISBN number is a valid number
If checksum is ‘x’ , then that represents the digit 10
Weight is associated with each digit
example: 1-214-02031-3
10 with the first digit
1 in the example
9 with the second digit
2 in the example
8 with the third digit
1 in the example
7 with the forth digit
4 in the example
6 with the fifth digit
0 in the example
5 with the sixth digit
2 in the example
4 with the seventh digit
0 in the example
3 with the eight digit
3 in the example
2 with the ninth digit
1 in the example
1 with the tenth digit
3 in the example
Checking valid
Multiply the digit by its weight and add the resulting products
If the sum is divisible by 11, then it is a valid ISBN number
example: 1-214-02031-3
1 * 10+2 * 9+1 * 8+4 * 7+0 * 6+2 * 5+0 * 4+3 * 3+1 * 2+3 * 1 = 88
88 is evenly divisible by 11, so the above number is a valid ISBN number
Main function
will input the number from the user into a character array
then call the function checkDigitswill return true
if there are digits
if a correct number of digits in each section
if each section is seperated by a ‘-‘
otherwise return false
If checkDigits returns true
main will call checkSumwhich will return true
if the sum of the products of the weights and digits is divisible by 11
otherwise return false
main program will output “valid” or “not valid”
*Do not use subscripted variables
*Use the pointers to the characters
Declaration for the checkSum
bool checkSum(char *);
*Note: *(ptrISBN +3) is the same as ptrISBN[3]
Check to see if there is an ‘-‘ in the i-th position
use this statement
if(*(ptrISBN + i) == ‘-‘)
Directions: input ISBN number from user and output "Valid" if the number is valid or “Invalid" otherwise. Terminate the input when the user enters "done" ISBN e number for a book 10 digit number made up of 4 sections . each section seperated by ‘ Example: ISBN number is: 1-214-02031-3 * 1 represents a country * must be 1 digit ° 214 represents the publisher e must be 3 digits * 02031 reprsents the book number must be 5 digits * 3 is the checksum * must be 1 digit or the letter'x' checksum is a digit to see if the ISBN number is a valid number * If checksum is 'x' , then that represents the digit 10
Explanation / Answer
Following is the c++ Program for ISBN:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
// remove dashes convert letters to upper case
string normalize(const string &isbn)
{
string ch;
for (char i : isbn) {
if (i == '-') {
continue; // if "-" then skip it
}
if (isalpha(i)) {
i = toupper(i); // Check uppercase
}
ch += i;
}
return ch;
}
// return the number of digits in to the string
size_t numDigits(string &str)
{
size_t numDigits = 0;
for (char ch : str) {
if (isdigit(ch)) {
++numDigits;
}
}
return numDigits;
}
enum ValidationCode {
Ok, // validation passed
NumDigits, // wrong number of digits
ExtraChars // extra characters in isbn
};
enum ValidationCode validate(string &isbn)
{
string normal = normalize(isbn);
size_t count = numDigits(normal);
if (count != 10) {
return NumDigits;
}
if (normal.size() == 10 ||
normal.size() == 11 && normal[10] == 'X') {
return Ok;
}
return ExtraChars;
}
main()
{
string str;
while (cin >> str) {
switch (validate(str)) {
case Ok:
cout << str << " is a valid isbn ";
break;
case NumDigits:
cout << str << " doesn't have 10 digits ";
break;
case ExtraChars:
cout << str << " has extra characters ";
break;
default:
cout << "ERROR: validate(" << str << ") return an unknown status ";
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.