tags:

views:

149

answers:

9

For my book class I'm storing an ISBN number and I need to validate the data entered so I decided to use enumeration. (First three inputs must be single digits, last one must be a letter or digit.) However, I'm wondering if it is even possible to enumerate numbers. Ive tried putting them in as regular integers, string style with double quotes, and char style with single quotes.

Example:

class Book{
public:
   enum ISBN_begin{
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
        };
   enum ISBN_last{
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', a, b, c, d, e, f, 
        g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z};

The compiler error says expected identifier which means that its seeing the numbers as values for the identifiers and not identifiers themselves. So is enumerating digits possible?

+1  A: 

You would have to do somethink like this:

   enum ISBN_begin {
            ZERO, ONE, TWO // etc.
        };
Andrew Hare
+1  A: 

I would suggest using a string for each of the begin/end criteria, ie:

string BeginCriteria = "0123456789";
string EndCriteria = "0123456789abcd... so forth";

// Now to validate the input    
// for the first three input characters:

if ( BeginCriteria.find( chInput ) != npos )
    // Then its good.

// For the last 3 characters:

if ( EndCriteria.find( chInput ) != npos )
    // Then its good.
DeusAduro
A: 

If you can't use regular expressions, why not just use an array of char instead? You could even use the same array, and just have a const index number where the ISBN_last chars begin in the array.

cfeduke
A: 

enums are not defined with literals, they are defined with variables

Finer Recliner
enumerator members are symbols
CiscoIPPhone
+7  A: 

I think you're going about this the wrong way...why not just use a simple regex that will validate the entire thing a bit more simply?

(yes, I know, that wasn't the original question, but it might make your life a lot easier.)

This page and this page provide some good examples on using regex to validate isbn numbers.

I think creating an enumeration whose values are equal to the entities they're enumerating...I think you're doing a lot more than you have to.

Beska
+4  A: 

Why would you want to enumerate numbers? Enums exist to give numbers a name. If you want to store a number, store an integer - or in this case a char (as you also need characters to be stored). For validation, accept a string and write a function like this:

bool ISBN_Validate(string val)
{
    if (!val.length == <length of ISBN>) return false;
    if (val[0] < '0' || val[0] > '9') return false;
    foreach (char ch in val)
    {
        if (ch is not between '0' and 'z') return false;
    }
}

Easy - and no silly enumerations ;)

nlaq
The purpose of enumerating the numbers was just to validate the data. I wasn't sure how to say if >a or <z, but I just realized that the less than and greater than operators sort alphabetically.
trikker
@trikker: This solution is right, but it's worth remembering that this will only work for latin characters and not other alphabets since not all encodings store characters as a contiguous sequence of codes.
sharptooth
I just ended up using !isdigit !isalpha
trikker
+1  A: 

enums really aren't what you want to use. They aren't sets like that. The members of an enum have to be symbols like variables or function names and you can give them values.

enum numbers { One = 1, Two, Three };

One after this is equivalent to a named constant with integer value 1. numbers is equivalent to a new type with a subrange of integer values.

What you probably want is to use a regular expression.

Rob K
+4  A: 
#include <ctype.h>

Don't forget the basics. The above include file gives you isalpha(), isdigit(), etc.

xcramps
A: 

Some languages (Ada for one) allows what you want, so your request is not too silly. You are simply forgetting that in C and C++, character literals are just another form of integer literals (of type int in C, of type char in C++)

AProgrammer