In: Computer Science
This C++ program is to validate the ISBN number and
output "Valid" if the number is valid, and "Invalid"
otherwise.
The ISBN number for a book is a 10-digit number made up of 4
sections separated by '-'. In the ISBN
number 1-214-02031-3:
1 represents a country (must be 1 digit)
214 represents the publisher (must be 3 digits)
02031 represents the book number (must be 5 digits)
3 is the checksum (must be 1 digit or the letter 'x').
The checksum is a digit to see if the ISBN number is a valid
number. If the checksum is 'x', then that
represents the digit 10.
A weight is associated with each digit:
10 with the first digit (1 in the example)
9 with the second digit (2 in the example)
8 with the 3rd digit (1 in the example)
7 with the 4th digit (4 in the example)
6 with the 5th digit (0 in the example)
5 with the 6th digit (2 in the example)
4 with the 7th digit (0 in the example)
3 with the 8th digit (3 in the example)
2 with the 9th digit (1 in the example)
1 with the 10th digit (3 in the example)
To check to see if an ISBN number is valid, you multiply the digit
by its weight and add the resulting
products. If the sum is evenly divisible by 11, then it is a valid
ISBN number.
In the 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
Since 88 is evenly divisible by 11, the above number is a valid
ISBN number.
The main function will create an array of pointers of type char
to store all the ISBN numbers I'll give
you in a separate text file for the convenience of copying. The
main should iterate over that array and for
each ISBN number call the function checkDigits which will return
true is there are digits and a correct
number of digits in each section, and if each section is separated
by a '-'. Otherwise, it will return false.
If checkDigits returns true, the main will call checkSum, which
will return true if the sum of the
products of the weights and digits is divisible by 11. Otherwise,
it returns false. The main program will
output "valid" or "not valid" for each ISBN number.
In this program, do not use subscripted variables. Use
the pointers to the characters. The declaration for
the checkSum would be:
bool checkSum(char *);
Note that functions take a single pointer as a parameter, not a
char ** (i.e. array of pointers).
Note, that *(ptrISBN + 3) is the same as ptrISBN[3]
To check to see if there is an '-' in the i-th position you can use
the statement:
if(*(ptrISBN + i) == '-')
Your program should not be like that:
if(ISBN[0] == '-' || ISBN[1] == '-' || ISBN[2] == '-' ….. ||
ISBN[10] == '-')
To check for each element in the array.
I want you to use the loop and iterate over the arrays using the
loop.
However, if you need to check just a couple (maybe three, max),
that is OK.
Should use:
2 functions with using const arguments
Using enum valid, invalid in place of bool
Do not use:
Declaring array according to standards
off-by-one errors
Using global variables
Run the program with the data from the data file
“test_data.txt”
Note, I want you to hard code the array of pointers. I don't want
you to read the data from the file on
the fly.
------------------------------------------------------------------------------------------------------------------------------------------------------------
Expected output would be similar to the following:
Input ISBN numbers:
(input number from given .txt file)
Array filled! //use a loop to get input from the user
Checking ISBN numbers...
//(for ISBN #'s return False/Invalid)
Invalid ISBN #
//(for ISBN #'s return True/Valid)
Checking sum of Valid ISBN numbers
Valid/Invalid ISBN # : (return sum of ISBN number)
------------------------------------------------------------------------------------------------------------------------------------------------------------
Given text file: Note, I want you to hard code the array
of pointers. I don't want you to read the data from the file
on
the fly. IF applicable/possible try to make another function to
show the difference.
1-214-02031-3
0-070-21604-5
2-14-241242-4
2-120-12311-x
0-534-95207-x
2-034-00312-2
1-013-10201-2
2-142-1223
3-001-0000a-4
done
// C++ program to validate ISBN
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
// enumeration to represent the result of validation
enum Result
{
Valid,
Invalid
};
// function declaration
Result checkDigits(const char *isbn) ;
Result checkSum(const char *isbn) ;
int main() {
// create an array of pointers to store the isbn numbers
const char *isbn[] = {"1-214-02031-3",
"0-070-21604-5",
"2-14-241242-4",
"2-120-12311-x",
"0-534-95207-x",
"2-034-00312-2",
"1-013-10201-2",
"2-142-1223",
"3-001-0000a-4"};
int size = 9;
// loop over the array to check if the ibsn are valid or not
for(int i=0;i<size;i++)
{
if(checkDigits(isbn[i]) == Valid )
{
if(checkSum(isbn[i]) == Valid)
cout<<isbn[i]<<" Valid"<<endl;
else
cout<<isbn[i]<<" Invalid"<<endl;
}else
cout<<isbn[i]<<" Invalid"<<endl;
}
return 0;
}
// function that returns true is there are digits and a correct
// number of digits in each section, and if each section is separated by a '-'.
// Otherwise, it will return false.
Result checkDigits(const char *isbn)
{
// check the length of the isbn
if(strlen(isbn) == 13)
{
// loop over the string
for(unsigned int i=0;i<strlen(isbn);i++)
{
// check if each section is separated by a '-'.
if(i==1 || i == 5 || i == 11)
{
if(*(isbn+i) != '-')
return Invalid;
} // check the last digit
else if(i == strlen(isbn)-1)
{
if((!isdigit(*(isbn+i))) && ((*(isbn+i)) != 'x'))
return Invalid;
}else // check if all the section except the last only consists of digits
{
if(!isdigit(*(isbn+i)))
return Invalid;
}
}
return Valid;
}
return Invalid;
}
// function that calculates the sum of multiplication of the digits and its weight
// and check if the result is divisible by 11
Result checkSum(const char *isbn)
{
int sum = 0;
int k = 10;
// loop over the string
for(unsigned int i=0;i<strlen(isbn);i++)
{
// ignore the separators
if((i != 1) && (i != 5) && (i != 11))
{
if(*(isbn+i) == 'x') // for the last digit, if x multiply the weight by 10
sum += k*10;
else // for the digits , multiply the value by its weight
sum += k*((int)(*(isbn+i) - '0'));
k--;
}
}
// check if sum is divisible by 11, return valid
if(sum%11 == 0)
return Valid;
return Invalid; // else invalid
}
//end of program
Output: