string splitting white space, comparing to strings, determine int intentions etc.

This is a discussion on string splitting white space, comparing to strings, determine int intentions etc. within the C++ Programming forums, part of the General Programming Boards category; Thanks =) Code: /* tbh I don't entirely understand what's happening here with the substr(0,1) thingy. My intentions here are ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    string splitting white space, comparing to strings, determine int intentions etc.

    Thanks =)
    Code:
     
    
    /* tbh I don't entirely understand what's happening here 
    with the substr(0,1) thingy. My intentions here are to 
    create a command line interface to help me edit certain 
    aspects of the "game" My first project is this SDL tile based 
    engine, and all I've used is the plain SDL library and have 
    been able to make map,collision,item,npc saving/loading,
     buttons, animations, chat boxes etc. (which I am intending 
    to use the chatbox as a console during development) this 
    being my first programming project other than the occasional
     bot scripting, my code looks primative, and i know, but it's all i 
    understand atm. So when I try to figure out how to do something 
    online, it usually doesn't work out how I expected, and I end up 
    spending hours trying to figure it out, and when I fail miserably i 
    post on a forum.
    */
     // STR_MESSAGES is the recently typed message into the chatbox
     //COM_LINE ="/";    
     // when I start my sentence with "/" it'll go through this function to    // determine what I am telling the program to do.
    
    
    ANString = STR_MESSAGES[2].substr (0,1);
          if (ANString == COM_LINE)  
    {  
               int TX_Coord = -1 ; 
    
               int TY_Coord = -1 ;
              
     std::stringstream os(STR_MESSAGES[2]);  // parses 's'
     std::string temporary;
          
             
          
          while (os >> temporary)                 // makes temp a token
          std::cout << temporary << endl;  //and deletes that token from itself 
    
    // this token comment, got it from searching how to do something.
    // still dont understand entirely what it means. so i left the // comment.
          
         
          bool TFSTATE = isInt(temporary); 
    // determine if the next "word" is just really a number.
    // and if it is a number, we are expecting it to be X Y coordinates
    // example "/ delete door 25 33"   
    
           if ( TFSTATE == true )  
          {  
               
               std::cout << "temp: " << temporary << endl;
               
                  if ( TX_Coord == -1) { TX_Coord = atoi (temporary.c_str()); } 
    
    // if the X value hasnt changed yet, this is probably what the input meant, so assign it.
    
    
             else if ( TX_Coord != -1) { TY_Coord = atoi (temporary.c_str()); }
    
    // if it has already changed, assign the next value to Y.
    
    
             else { MessageBox(NULL,"TX and TY Coord already taken!","Command Line",MB_SETFOREGROUND); }
    
    // if input has more than 2 seperate numbers, was obviously 
    a user error.
                 
                 std::cout << "X: " << TX_Coord << "Y: " << TY_Coord << endl;
               
          }
         
        else if ( TFSTATE == false )
        { 
          /////////////////////////////////////////////////////// 
               if (temporary == CREATE)   { ACTION = 1;               std::cout << "create ";}
          else if (temporary == ERASE)    { ACTION = 2;               std::cout << "erase "; }
          else if (temporary == TELEPORT) { ACTION = 3;               std::cout << "teleport "; }
          ///////////////////////////////////////////////////////  
          
    
    
          if      (temporary == NPC) { CLASS = 2;                     std::cout << "npc "; }
          else if (temporary == CONTAINER) { CLASS = 3;               std::cout << "container "; }
          else if (temporary == DOOR) { CLASS = 4;                    std::cout << "door "; }
          
    /* this is what I type, and this is what I get. 
    / create door 12 34  
    / delete npc 54 23  
    */
    
    /*
    
    /
    create
    door
    12
    34
    temp: 34
    X: 34 Y: -1
    /
    delete
    npc
    54
    23
    temp: 23
    X: 23 Y: -1
          */
    Last edited by Caleb Willette; 09-10-2012 at 01:08 PM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Just realized my else for the message box is written improperly, but that's not the problem =p.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    1) Don't embed your problem explanation inside CODE Tags
    2) Since we don't know what you should get, we don't know what's wrong with what you did get.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    One problem is with your while loop.

    when you write:

    Code:
    while (os >> temporary) 
       std::cout << temporary << endl;
    it is the same as:

    Code:
    while (os >> temporary)
    { 
       std::cout << temporary << endl;
    }
    This will print all tokens in the stringstream, and the last valid token will be saved in temporary when the loop exits. This is why your output says "temp: " and the last, in this case number, you wrote in.

    I don't really get the entire logic scheme of your code, so it is hard to give more input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting a space delimited string
    By lukeaar in forum C Programming
    Replies: 5
    Last Post: 05-15-2010, 02:33 AM
  2. Read single words seperated by white space in as strings
    By steals10304 in forum C Programming
    Replies: 2
    Last Post: 11-02-2009, 03:05 PM
  3. Strings and white space
    By ginom71 in forum C Programming
    Replies: 13
    Last Post: 07-12-2009, 09:48 AM
  4. Reading string with white space in C
    By gep in forum C Programming
    Replies: 2
    Last Post: 05-30-2009, 05:18 PM
  5. Scanning Strings till no white space is found?
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 02:02 PM

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