I've been given a homework assignment that goes as follows:
We will write a program which inputs a 10-place ISBN number, cleans
it up (removes spaces, and punctuation,) then strips the last digit
(saving it) then re-calculates that last (check) digit from the rest of
the number. If the result of our calculation matches the saved check
digit, the function will validate the ISBN number, otherwise the function
will sound an alarm. See the bottom of this file for some sample runs.
NOTE: THE LAST (CHECK) DIGIT CAN BE EITHER A DIGIT OR AN 'X'. That
requires care on your part. Don't toss out a valid 'x' just because
it's not a digit.
I want to take it one step at a time so I am trying to test if I can intake a string and clean it up first.
I wrote up this code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std ;
int main ()
{
char promptisbn [] = "Plese Enter 10 Digit ISBN" ;
string isbn ;
int x = 0 ;
cout << promptisbn << endl ;
getline (cin, isbn) ;
for ( x ; x < isbn.length() ; x++ )
if ( !isdigit(isbn[x] && toupper(isbn[x])!= 'X') )
isbn.erase (x,1) ;
cout << isbn ;
getchar () ;
}
And if I input this number:
ISBN 0-321-40939-6
I get a return value of:
SN0314996
It's cleaning up every other number... I'm not sure why :/ I think it has something to do with the:
isbn.erase (x,1)
I've tried changing it to (x,0) & (x) and (x,2) to see what it does. But I cannot figure out how to make it just go through and erase the characters I don't want.
I appreciate your time and any insight you may have. Thank you so much.
`B