Oct 25, 2012 at 3:29am UTC
any help given is MUCH APPRECIATED! (code below)
Write a program that tests an ISBN to see if it is valid.
a. For an ISBN number to be valid, the weighted sum of the 10 digits must be evenly
divisible by 11.
b. The tenth digit may be X, which indicates 10.
2. To determine the weighted sum, the value of each position is multiplied by its relative position,
starting from the right, and the sum of the products is determined.
3. For explanation purposes to understand how the weighted sum is calculated, the calculation of the weighted sum for the ISBN shown above is shown in the following table:
isbn calculated like if it's 0789 and those are the first numbers out of the 10 then it's 0*10 ,7 * 9, 8* 8, and so onnn
Since the weighted sum calculated = 110, the weighted sum modulus 11 is therefore zero, making this ISBN number valid.
5. Input and validation necessary:
a. That a 10 digit number (and possibly an “X”) has been entered.
b. That none of the values entered are non-numeric.
c. That no decimals have been entered.
d. Note: The input values entered do not have to include the dashes as shown in the examples and the test cases.
6. Additional Processing:
a. A loop should be built so that the program can process an unknown quantity of ISBN
validations by prompting the user if they wish to process another ISBN.
(1) The user can enter a “Y” or “y” as valid responses to process another ISBN.
(2) When a user enters anything else, the program should end and a message should display that states: “Program successfully ended.”
a. A heading explaining the contents of the output.
b. Display one line per test case containing the ISBN and whether the ISBN is valid or
This is my code.. it keeps giving me the result "valid" even if i enter a number that sum isnt divisible by 11.... any help given is MUCH APPRECIATED!
using namespace std;
int weight = 1;
int sum = 0;
const char X = 10;
cout << "Please put in ten digit ISBN code: " ;
cin >> isbncode;
cout << endl;
for( counter = 1; counter <= 10; counter++)
extracted_number = isbncode % 10;
other_number = counter * extracted_number;
sum = sum + other_number;
if (sum % 11 == 0)
cout << " this is a valid number " << endl;
cout << " is not divisible by 11 " << endl;
cout << " would you like to enter another isbn code: " ;
cin >> otherisbn;
cout << endl;
while (otherisbn == 'Y' || otherisbn == 'y');
Oct 25, 2012 at 7:55am UTC
everytime the for loop executes, the isbncode(for example:12345)is same 12345. you always will be extracting number 5 from the isbncode.
So you need to take
'5' first time
'4' second time
'3' third time
'2' fourth time
'1' last time
after that loop will be exited.
To do this just include the below statement after the extracted_number calculation
Oct 25, 2012 at 2:05pm UTC
Thanks vichu88 it worked!