C++ isbn validator

This is a little C++ function/method I wrote to validate ISBN 10 numbers. It takes in a string without '-' or ' ' and returns true if valid and false if not. Only replace the [REPLACE] tag with your class name and off you go. Is not the quickest way to do it, but it is easy to understand and KISS is what we all whant isn't it.

 1:   /**
2: Checks if the isbn is valid
3: */

4: bool [REPLACE]::isbnOk(std::string isbnToTest){
5:
6: /*The multiplyer */
7: int multy = 10;
8:
9: int sum = 0;
10:
11: int checkSum = 0;
12:
13: /* Will work only for ISBN 10 */
14: if(isbnToTest.size() != 10){
15: std::cout << "ISBN wrong size only isbn 10" << std::endl;
16: return false;
17: }
18:
19: for(int i = 0; i < 9; i++){
20: /* Because atio thinks it is a pointer to an array it
will shoot off into memory. So if you want to convert
a single char into an int don't use atoi. IMPORTANT*/

23: int atoiIsGay = isbnToTest[i] - '0';
24: sum = sum + (atoiIsGay * multy);
25: multy--;
26: }
27:
28: checkSum = (11 - (sum % 11));
29:
30: if(checkSum == 10 && isbnToTest[9]=='X')
31: return true;
32:
33: if(checkSum == (isbnToTest[9] - '0'))
34: return true;
35:
36: return false;
37: }

And the clean isbn method to get rid off all the stuff we don't want

 1:   /**
2: Cleans out the isbn, Removes spaces and stuff
3: */

4: std::string [REPLACE]::cleanIsbn(std::string dirtyIsbn){
5: std::string outPutBuffer = "";
6:
7: for (unsigned int i =0; i < dirtyIsbn.size(); i++){
8: if((dirtyIsbn[i] != '-') && (dirtyIsbn[i] != ' ')){
9: outPutBuffer += dirtyIsbn[i] ;
10: }
11: }
12: return outPutBuffer;
13: }

No comments: