SYNTAX LANGUAGE: C++ I need help with a programming assignment. The program\'s o
ID: 642165 • Letter: S
Question
SYNTAX LANGUAGE: C++
I need help with a programming assignment. The program's objective is to input ISBN number from a user and output "Valid" if the number is valid & "Invalid" if otherwise. Terminate when the user enters "done". 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 a 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. Please liberally apply COMMENTS IN THE CODE to explain your steps, I MUST LEARN by seeing what you are doing. Thank You!
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
bool isCorrectISBNCode(string isbn){
// The value associated with each digit
int value = 10;
// SUM
int sum = 0 ;
// Loop runs over the entire string(ISBN Code)
for(int i = 0 ; i < 13; i++){
//if it's a number
if(isbn[i] != '-'){
int digit = isbn[i] - '0';
sum += value*digit;
value--;
//value decreases by 1
}
}
//Check if it's valid
if(sum%11 == 0)
return true;
return false;
}
int main(){
string isbn;
string userChoice;
do{
cout << "ISBN Code: ";
cin >> isbn;
if(isCorrectISBNCode(isbn))
cout << "YES" << endl;
else
cout<< "NO" << endl;
cout << "CONTINUE ? (yes/done) ";
cin >> userChoice;
}while(userChoice != "done");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.