Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A check digit (aka checksum digit) is added to a number, such as book ISBN number or bank routing number, to verify the correctness of

A check digit (aka checksum digit) is added to a number, such as book ISBN number or bank routing number, to verify the correctness of the number. The check digit is usually added as the last digit in the number. A common usage of a check digit is to detect errors made while entering the number into a system. For this assignment, you will write a C++ program to generate check digits for ISBN-13 numbers.

The ISBN-13 is a standard, introduced in 2007, for identifying books. It uses 13 digits d0d1d2d3d4d5d6d7d8d9d10d11d12. The last digit d12 (i.e., the 13th digit) is check digit that is calculated from the other digits using the following formula:

10 - (d0 + d1 * 3 + d2 + d3 * 3 + d4 + d5 * 3 + d6 + d7 * 3 + d8 + d9 * 3 + d10 + d11 * 3) % 10

If the checksum is 10, replace it with 0.

Your code must have a function that accepts a string parameter of the first 12-digits in an ISBN-13 number and returns the checksum digit (i.e., the 13th digit). You may use other functions if you like. For example, a helpful function takes a character containing a digit as parameter and returns that digit as an integer, which you can define as follows:

int toDigit(char ch){ return ch - '0'; // ascii values are subtracted } 

The program must read input from a file, named isbn.txt, that is located in the same folder as the program. Each line on the file contains the first 12-digits of a valid ISBN-13 number. The program outputs to the computer's screen the checksum digit for each number in the file. For example, using the following input file:

978013213079 978032156384 978007063546 978032126817 978145169885 978013213080 978160469555 978032133487 978147926720 

your program should produce the following output:

0 2 3 4 5 6 7 9 0 

Here is code.

#include #include #include

using namespace std;

int checkSum(string s) { istringstream iss(s); long n; int check_digit = 0; iss >> n; while(n > 0) { check_digit = check_digit + (n % 10) * 3; n = n / 10; } check_digit = 10 - (check_digit % 10); check_digit = check_digit % 10; return check_digit; }

int main() { ifstream inFile;

inFile.open("isbn.txt"); string s; while(inFile >> s) cout << checkSum(s) << endl; inFile.close(); return 0; }

But the output is

222222222

Please help me out!!!

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

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago