0

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. :)

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

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

Edited by Ancient Dragon: n/a

0

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

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.