Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this home work, you have to use C++ loops, functions, and C++ string Name your C++ file as CIS22A-HW6-XXXXX.cpp . The book ISBN code

In this home work, you have to use C++ loops, functions, and C++ string

Name your C++ file as CIS22A-HW6-XXXXX.cpp.

The book ISBN code

The International Standard Book Number (ISBN) is a unique numeric commercial book identifier based upon the 9-digit Standard Book Numbering (SBN) code created by Gordon Foster, Emeritus Professor of Statistics at Trinity College, Dublin for the booksellers and stationers W. H. Smith and others in 1965.

A 10-digit ISBN book number consists of 10 digits

D0D1D2D3D4D5D6D7D8D9

The last digit D9 is a checksum which is calculated from the first 9 digits using this formula:

D9 = (D0 x 1 + D1 x 2 + D2 x 3 + D3 x 4 + D4 x 5 + D5 x 6 +

D6 x 7 + D7 x 8 + D8 x 9) modulus 11

If the checksum is 10, the last character D9 is set to X because we can not store 10 as a single digit. The modulus operation is the remainder operation

Write a program CIS22A-HW6-XXXXX.cpp (XXXXX are the last 5 digits of your ID) that prompts the user to enter the first 9 digits of an ISBN and displays the complete 10-digit ISBN code including all 0.

Requirements:

Do the following to earn points for the home work:

1- Use the C++ string function at(n) to get the character at position n where n is from 0 to string length - 1.

For example:

string str = "ABCDEF";

int i = 3;

char c = str.at(i); // get the fourth character of the string

See http://www.cplusplus.com/reference/string/string/at/

2- Define a C++ function to validate the user input:

bool validateInput(string isbn) {

.

return

}

This function returns true if the input is valid and returns false if it is not valid. Things that need to be checked:

- Input length must be exactly 9. Check this item first because it will be a waste to check the next item that requires a loop if this check fails.

- Each character must be a digit. Use a loop for this purpose to go through each character.

This function is called from main()

3- Define a C++ function to calculate the checksum. This function must a contain a loop to go through each digit of the ISBN code. The function must return a character result ('0' through '9', or an 'X'). This function must be able to return an 'X' because the modulus 11 operation can results in a 10, that we will need to turn into an X.

char getCheckSum(string isbn) {

// Calculate the check sum

char result;

// Fill in your code

// loop

..

return result;

}

This function is called from main().

4- Note that if a character c contains the character 5, then the ASCII code of it is 53. If you use c directly in calculation, the value 53 is used, not value 5. See http://www.asciitable.com/. To get the digit value 5 from character '5', calculate the offset of character c from character '0':

int value = c - '0';

6- Create function prototypes and put them at the top of the program as mentioned in the slides of chapter 6. Put all complete function definition code AFTER main().

7- Write an additional function formatWithHyphen() that takes the 10-digit ISBN code and returns an ISBN string with 3 embedded - after the first digit, after the fourth digit and the ninth digit position. For example, this function will take a parameter containing 0306406152 and returns a string 0-306-40615-2

string formatWithHyphen(string isbn)

This function returns a string and does NOT display any output. You can add the following statement in main() to call this function to display the ISBN code with the extra hyphens in addition to the original 10-digit ISBN.

cout << " The hyphen-formatted ISBN code is " << formatWithHyphen(isbn + extra) << endl;

There are 2 different good ways to code this function, any way it will not require a loop, just a single statement will suffice:

Use the string insert() function

http://www.cplusplus.com/reference/string/string/insert/

Use the string concatenation (+ operator) and substr() function

http://www.cplusplus.com/reference/string/string/substr/

8- Only the main() function can contain "cin" and "cout"

9- DO NOT use C-style character array

----------------------------------------------

Here is a simple main() function that you can use for your program:

int main()

{

string isbn;

cout <<

"This program calculates the checksum of a 9-digit ISBN code and display the 10-digit ISBN."

<< endl;

cout << "Enther the 9 digits for the ISBN code or X to exit: ";

cin >> isbn;

while (isbn != "X")

{

bool isValid = validateInput(isbn);

if (!isValid)

{

cout << " The input ISBN code is not valid. ";

}

else

{

char extra = getCheckSum(isbn);

cout << " The 10-digit ISBN code is " << isbn + extra << endl;

cout << " The hyphen-formatted ISBN code is " <<

formatWithHyphen (isbn + extra) << endl;

}

cout << "Enther the 9 digits for the ISBN code or X to exit: ";

cin >> isbn;

}

}

Test your code and compare with the posted results.

Sample test run:

This program calculates the checksum of a 9-digit ISBN code and display the whole 10-digit ISBN.

Enter 9 digits for the ISBN code or X to exit: 123456789

The 10-digit ISBN code is 123456789X

The hyphenated ISBN code is 1-234-56789-X

----------------------

Enter 9 digits for the ISBN code or X to exit: 124563299

The 10-digit ISBN code is 124563299X

The hyphenated ISBN code is 1-245-63299-X

----------------------

Enter 9 digits for the ISBN code or X to exit: 111111111

The 10-digit ISBN code is 1111111111

The hyphenated ISBN code is 1-111-11111-1

----------------------

Enter 9 digits for the ISBN code or X to exit: 030640615

The 10-digit ISBN code is 0306406152

The hyphenated ISBN code is 0-306-40615-2

----------------------

Enter 9 digits for the ISBN code or X to exit: 5456456

The input ISBN code is not valid.

----------------------

Enter 9 digits for the ISBN code or X to exit: 34567g124

The input ISBN code is not valid.

----------------------

Enter 9 digits for the ISBN code or X to exit: X

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions