Question

In: Computer Science

This C++ program is to validate the ISBN number and output "Valid" if the number is...

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

Solutions

Expert Solution

// 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:


Related Solutions

C++ Here is a program that to validate the ISBN number from user typed and output...
C++ Here is a program that to validate the ISBN number from user typed and output "Valid" if the number is valid, and "Invalid" otherwise. I want to change this program to read the file, and to validate the ISBN number from the file. Please add comments. #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...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
C++ program to accept characters as a polynomial and then output whether they're valid or not....
C++ program to accept characters as a polynomial and then output whether they're valid or not. Types of polynomials that don't work: n^5n, n^9.7, 8n, n^7-7*n
Write an ISBN Validator in C language What are ISBN? ISBN, or International Standard Book Number,...
Write an ISBN Validator in C language What are ISBN? ISBN, or International Standard Book Number, is a numeric identifier for commercially printed books. Prior to 2007, they had 10 digits, but all newly issued ISBN use 13 digits. They are used to uniquely identify a book and consist of several portions. These include the registrant group, publisher, title, and a check digit. The registrant group indicates where the publisher is located. For example a registrant group of 0 or...
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to...
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to be ? Solve the congruence 121x ≡ 5 mod 350.
Write a C program for a library automation which gets the ISBN number, name, author and...
Write a C program for a library automation which gets the ISBN number, name, author and publication year of the books in the library. The status will be filled by the program as follows: if publication year before 1985 the status is reference else status is available. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays, and deletes the elements from the menu by selecting options....
Run the program and enter domain names to validate. Note that even valid input is flagged...
Run the program and enter domain names to validate. Note that even valid input is flagged as invalid. Change the program to validate a domain name. A valid domain name for this program has a second-level domain followed by a core gTLD. Run the program again. import java.util.Scanner; public class CoreGtldValidation {    public static void main (String [ ] args) { Scanner scnr = new Scanner(System.in); String coreGtld1; String coreGtld2; String coreGtld3; String coreGtld4; String inputName; String searchName; String...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT