Basic string question

This is a discussion on Basic string question within the C++ Programming forums, part of the General Programming Boards category; Can anyone please explain to me why in the hell this isn't working... I want it to start at the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Basic string question

    Can anyone please explain to me why in the hell this isn't working... I want it to start at the beginning of the do loop if there is more than 1 character in the string, or if the character entered cannot be evaluated to a number from 1-9, but at the moment, if I enter something like:

    1saisagiusgqje2132t1ge

    It will register as 1.

    I honestly can't see ANYTHING wrong with what I have, but I might just point out it's 3:00AM, and I'm tired, and I've probably got some ridiculously easy completely stupid mistake that I just cannot see.

    I'm compiling with g++ on a Unix system. It's going to be a Tick Tack Toe game (I'm extremely bored)...

    Here is the problematic snippet of code:

    Code:
     do {
                    cout << "Enter a board position number: ";
                    cin >> strpos;
                    pos = atoi(strpos.c_str());
                    if (strpos.size() > 1 || !pos)
                            continue;
            } while (!CheckSquare(pos, board, 'x'));
    And here is the rest of what I have so far if you're interested:

    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    using namespace std;
    
    int CheckSquare(int square, char ** board, char player);
    
    int main() {
    
            string strpos;
            int pos;
    
            char ** board;
            board = new char*[3];
            for (int i = 0; i < 3; i++)
                    board[i] = new char[3];
    
            for (int i = 0; i < 3; i++)
                    for (int y = 0; y < 3; y++)
                            board[i][y] = '-';
    
            do {
                    cout << "Enter a board position number: ";
                    cin >> strpos;
                    pos = atoi(strpos.c_str());
                    if (strpos.size() > 1 || !pos)
                            continue;
            } while (!CheckSquare(pos, board, 'x'));
    
            for (int i = 0; i < 3; i++)
                    for (int y = 0; y < 3; y++)
                            cout << board[i][y] << " ";
    
            for (int i = 0; i < 3; i++)
                    delete [] board[i];
            delete [] board;
    
    }
    
    int CheckSquare(int square, char ** board, char player) {
    
            int down;
            int side;
    
            if (square >= 7) {
                    down = 2;
                    side = square - 7;
            }
            else if (square >= 4) {
                    down = 1;
                    side = square - 4;
            }
            else {
                    down = 0;
                    side = square - 1;
            }
    
            if (board[down][side] == '-')
                    board[down][side] =  player;
    
            else
                    return 0;
    }
    Thanks.

    P.S I'm probably going to end up feeling completely stupid, but oh well... I have no idea why I can't figure this out HAHA!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears that the continue keyword doesn't skip checking for the end condition of a loop, if that's what you meant. You probably might use a loop within a loop if you want to loop until certain conditions are met and only then check other condition as well.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    What you missed is that atoi() grabs as much as it can to convert to an int. In the case of "1saisagiusgqje2132t1ge", that is 1, so it returns 1.

    As alternatives, consider using a stringstream or boost::lexical_cast instead of atoi().

    By the way, why do you manually manage the memory for a char** when you can use a vector (of vectors)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    There are several find functions in basic_string that will assist you.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by laserlight View Post
    What you missed is that atoi() grabs as much as it can to convert to an int. In the case of "1saisagiusgqje2132t1ge", that is 1, so it returns 1.

    As alternatives, consider using a stringstream or boost::lexical_cast instead of atoi().

    By the way, why do you manually manage the memory for a char** when you can use a vector (of vectors)?
    That may be so, but why would string.length() return 1 or less than 1 even when the string has over 1 characters in it. Even if atoi() grabs as much as it can to convert to an int, the call to "string.length() > 1" should still return true.

    Oh and, I thought it would be more interesting to manually manage the memory for a 2 dimensional array instead of using a vector of vectors ;p
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by pobri19
    That may be so, but why would string.length() return 1 or less than 1 even when the string has over 1 characters in it. Even if atoi() grabs as much as it can to convert to an int, the call to "string.length() > 1" should still return true.
    No, length() is indeed returning a value greater than 1. The "problem" is that continue causes control to go to the end of the loop. This means that the condition is evaluated immediately as the next iteration begins. As such, if CheckSquare() returns a true/false value, the loop terminates/continues regardless of the continue statement.

    What you should do to fix that is this:
    Code:
    do {
        cout << "Enter a board position number: ";
        cin >> strpos;
        pos = atoi(strpos.c_str());
    } while ((strpos.size() > 1 || !pos) || !CheckSquare(pos, board, 'x'));
    By the way, it looks like CheckSquare() has a control path that does not return a value. Also, perhaps it should return a bool instead of an int?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Oh I understand, thanks for the help!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21