Hey! I've got a little problem I have no idea where to begin with.

I've got a string and I need to check the insides of it. The string should be formatted like <letter><number>, for example "A1" or "F4". The letters can only be from A to F and numbers from 1 to 4. So... How is the checking done? I thought I should start with checking if the string is just two characters long, then tokenize the characters for per-character checking. But, the actual check-code isn't clear. :)

Recommended Answers

All 4 Replies

All you need is three very simple if statements, something like below

if string length != 2 then error
if string[0] < 'A' || string[0] > 'F' then error
if string]1] < '1' || string[1] > '4' then error

Ah, thank you! I forgot they can actually be used like arrays. Thanks :)

Umm, now that I try to code this, I don't get the right true / false values.

Here's my code:

string arg2 = argv[2];

if((arg2.length() == 2) && (arg2[0] >= 'A') && (arg2[0] <= 'F') && (arg2[1] >= 1) && (arg2[1] <= 4)) {
            ok2 = true;
            cout << "OK2" << endl;
        }else{
            cout << "arg2.length() == " << arg2.length() << endl;
            cout << "arg2[0] == " << arg2[0] << endl;
            cout << "arg2[1] == " << arg2[1] << endl;
        }

This prints out the following:
arg2.length() == 2
arg2[0] == A
arg2[1] == 1

So... where's the problem here? Is there something wrong with the if sentence?

on line 3: 1 and 4 have to be enclosed in quotes just like A and F.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.