trikker
trikker

Reputation: 2709

Enumerating digits

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?

Upvotes: 0

Views: 621

Answers (9)

Andrew Hare
Andrew Hare

Reputation: 351526

You would have to do something like this:

   enum ISBN_begin {
            ZERO, ONE, TWO // etc.
        };

Upvotes: 1

xcramps
xcramps

Reputation: 1213

#include <ctype.h>

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

Upvotes: 4

AProgrammer
AProgrammer

Reputation: 52294

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++)

Upvotes: 0

Rob K
Rob K

Reputation: 8926

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.

Upvotes: 1

user19302
user19302

Reputation:

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 ;)

Upvotes: 4

Beska
Beska

Reputation: 12667

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.

Upvotes: 6

Finer Recliner
Finer Recliner

Reputation: 1597

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

Upvotes: 0

cfeduke
cfeduke

Reputation: 23226

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.

Upvotes: 0

DeusAduro
DeusAduro

Reputation: 6076

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.

Upvotes: 1

Related Questions