Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Strings.... 1

Status
Not open for further replies.

chmilz

Programmer
Jan 10, 2001
94
CA
Hi!

I am writing a program that will keep track of books through their ISBN numbers. However, I have a function that will test to ensure that the string IS a valid ISBN Number and thats where I am having trouble. My function prototype is:

static bool IsValidISBN(const CString & sISBN)
//sISBN is the name of the string
{

}
there are three criteria the string has to meet before it is assumed valid:
1) The string must be exactly 13 char's long
2) It must have three dashes in the correct spots
3) There cannot be two dashes in a row
This is what a VALID ISBN looks like: 0-672-30787-1
If you could give me some help on developing code for this function I would REALLY appreciate it!

Thank You!!!
 
Hmmm... Well, I'd recommend using some of the CString class member functions like GetLength(), Find() and perhaps the overloaded subscript operator[].

The Find function can be particularly useful as it can be used to search for a single character or substring within a string (so you could look for '-' and/or "--") and you can specify where in the string to start searching if you provide a second parameter.

However, if you want to do a proper job of validating an ISBN, just checking for length and dash positions won't be enough. The following should be taken into considered when you write your validation (incidentally, I happen to have a copy of Mickey William's Essential Visual C++ 4 handy - I'll use it's ISBN in describing how to validate the check digit):

International Standard Book Numbers consist of exactly 13 characters: ten digits plus three dashes. The three dashes are used to create four groups of digits:
- The first group of digits indicates the language the book was written in or the language of the country the book was published in. One-digit numbers are used for common languages such as English (0 or 1), French (2), German (3) or Spanish (4). Multi-digit numbers are used for smaller language areas such as the Dutch speaking area (90) or countries like Surinam (9942).
- The second group of digits indicates the publisher (larger publishers have shorter numbers).
- The third group of digits forms the book number as determined by the publisher.
- The last group is a one-character check-digit.

The check-digit is computed as follows: multiply the first digit of the ISBN by 10, the second by 9, the third by 8, etc until… the ninth by 2. Add the results together and subtract the sum from the next-highest multiple of 11. The result of this last subtraction will be the check-digit unless the result equals 10 in which case the check-digit will be the character ‘X’.

For example, the check-digit of the ISBN of Mickey William's Essential Visual C++ 4 (0-672-30787-1) can be calculated as follows:
Sum = 10x0 + 9x6 + 8x7 + 7x2 + 6x3 + 5x0 + 4x7 + 3x8 + 2x7
= 0 + 54 + 56 + 14 + 18 + 0 + 28 + 24 + 14
= 208
The next highest multiple of 11 is 209, therefore the check-digit equals 209 – 208 = 1.
 
Just in case that didn't help, this code should work:
Code:
bool
Code:
 IsValidISBN(
Code:
const
Code:
 CString& sISBN)
{
Code:
bool
Code:
 bValid = false;
Code:
if
Code:
(sISBN.GetLength() == 13)
  {
Code:
// Determine if hyphens are correct
Code:
Code:
if
Code:
(sISBN.Find('-') > 0 && sISBN[11] == '-' && sISBN.Find("--") == -1)
    {
      CString sTemp = sISBN;
      sTemp.MakeUpper();
Code:
// Just in case last char is 'x'
Code:
Code:
// Now confirm that there are only three hyphens
Code:
Code:
if
Code:
(sTemp.Remove('-') == 3)
      {
Code:
// First nine characters must be numeric
Code:
        CString sNumeric = "0123456789";
Code:
bool
Code:
 bCharsValid =
Code:
true
Code:
;
Code:
int
Code:
nMult = 10, nTotal = 0, nValue;
Code:
for
Code:
(
Code:
int
Code:
nSub = 0; nSub < 10 && bCharsValid; nSub++)
        {
          nValue = sNumeric.Find(sTemp[nSub]);
Code:
// If char is numeric, nValue has its numeric equivalent
Code:
Code:
// If not then nValue will be -1
Code:
Code:
if
Code:
(nValue ==  -1)
            bCharsValid =
Code:
false
Code:
;
Code:
else
Code:
          {
            nTotal += nValue * nMult;
            nMult--;
          }
Code:
// Last character can also be 'X'
Code:
Code:
if
Code:
(nSub == 8)
            sNumeric += &quot;X&quot;;
        }
Code:
if
Code:
(bCharsValid && (nTotal % 11 == 0))
          bValid =
Code:
true
Code:
;
      }
    }
  }
Code:
return
Code:
bValid;
}
 
Hi

I am trying to search for Data within a text file that uses , to denote a column and there is a carriage return at the end of each line. I need to check the validity of about 12 items in a text file.

I can find some elements in the text file using searchchr however I can't use the CString functions as I need to convert the results of a fopen() from CHAR[110] to a String (I am a beginner and a little confused..).

How would you use the above function to check for valid ISBN's in a text file and return the invalid ISBN's (either to a different text file or to a dialogue box)?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top