0

I need to validate one input string from a user. Eventually it will need to break down into two coordinates. ie a4 c3. And once they are coordinates they need to be broken out into 4 separate ints. a=0 b=1, etc. They must also follow the following stipulations:

If an end-of-input signal is reached the program quits. Otherwise, all non-alphanumeric characters are discarded from the input. If what remains is the single letter 'Q' Then the program quits. If what remains consists of 4 characters, with one letter and one digit among the first two characters and one letter and one digit among the last two characters, and if each letter-digit pair is in the legal range for our grid Then input is acceptable.

I have completely over-thought and ruined my function. Please let me know where I can make some corrections.

I am mainly having trouble going from one string, to four chars if and only if the data is valid. Everything else I can handle.

Here is what I have so far.

void Grid::playerMove()
{
    string rawMove;
    string pair1 = "  ";
    string pair2 = "  ";
    bool goodInput = false;
    char maxChar = 'a';
    char chary1, chary2;
    int x11,x22,y11,y22;
    for (int i =0; i<size; i++)
    {
        maxChar++;
    }

    while(!goodInput)
    {
        cout<<"What two dots would you like to connect? (Q to quit) ";
        cin>>rawMove;
        rawMove = reduceWords(rawMove);
        if (rawMove == "Q")
        {
            cout<<"end game";
            goodInput = false;
        }
        else if (rawMove.size() == 4)
        {
            for(int j=0;j<2;j++)
            {
                if (pair1[j] >='a' && pair1[j] <=maxChar)
                {
                    chary1 = pair1[j];
                }
                else if(pair1[j] >=0 && pairl[j]<=size+1)
                {
                    x1 = pair1[j];
                }
            }
        for(int k=0;k<2;k++)
        {
            if (pair2[k] >='a' && pair2[k] <=maxChar)
            {
                chary2 = pair2[k];
            }
            else if(pair2[k] >=0 && pair2[k]<=size+1)
            {
                x2 = pair2[k];
            }
        }
    }
    if(char1 != NULL && char2 != NULL && x1 !=NULL && x2 != NULL)
    {
        for (int m = 0; m <= size m++)
        {
            if (char1 == m;)
            {
                x1 = m;
            }
        }
        for (int n = 0; n <= size n++)
        {
            if (char2 == n)
            {
                x2 = n;
            }
        }
    }
}

The end goal would be to have x1, x2, y1, and y2 with their respective values.

Keep in mind I am not allowed to have any external libraries.

3
  • 4
    I would suggest <regex>. It's part of the language standard.
    – Kerrek SB
    Dec 9, 2011 at 1:31
  • Ok, normal pitfalls check, you are aware that cin stops on whitespace? What is reduceWords, how is it defined?
    – r_ahlskog
    Dec 9, 2011 at 6:36
  • Does this even compile right now? Char1, Char2, x1, & x2 are not defined anywhere (from what I can see).
    – Aaron
    Dec 9, 2011 at 13:51

2 Answers 2

1

It's not clear what exactly you want to achieve, but here are some pointers to get you started:

  1. The while loop will never end because you're setting goodInput to false on quit which lets the loop continue.

  2. The code probably does not even compile? You are missing a curly closing brace..

  3. You are initializing pair1 and pair2 to empty strings but never change them again, so they will never contain any real information about your moves

  4. maybe what you really want is to split up rawMove into the pair1 and pair2 substrings first?

0

Since this is a homework - and you're supposed to learn from those (right?) - I'm not going to give you the complete answer, but rather something like a recipe:

  • Use std::istream::getline(char*, std::streamsize s) to read a whole line from std::cin. Make sure you allocate a buffer large enough to hold the expected input (including the terminating null character) plus some more for invalid characters. After the call, check the failbit (input was too long) and the eofbit (hit the end-of-input) of the std::cin stream and handle those cases. Construct a std::string from the buffer if there was no error or EOF has not been reached.

  • Write a character-classification function (e.g. call it isAlNum(char c)) that returns true if the char argument is alpha-numeric, and false otherwise.

  • Combine std::string::erase(), std::remove_if(), std::not1(), std::ptr_fun() and your function isAlNum() to sanitise the input string.

  • Write a function that validates and parses the coordinates from the sanitised input string and call it with the sanitised input string.

  • Wrap the whole thing in an appropriate while() loop.

This should get you started in the right direction. Of course, if you're allowed to use C++11 features and you know how to write good regular expressions, by all means, use the <regex> header instead of doing the parsing manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.