GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Sep-2007, 17:36
baka101 baka101 is offline
New Member
 
Join Date: Sep 2007
Posts: 2
baka101 is on a distinguished road

Help with parsing a string.


I am trying to parse a user-entered string and separate it at all the spaces, but I don't know how to change the parameters to accept user-entered strings, just what string is programmed in for the string already.

I did not create this code, but I am trying to modify it to work like I want it to, so don't ask me the how the whole thing works.


Here is the code I am working on:

CPP / C++ / C Code:
//paerser class--------------------------------------------
class parser 
{
      std::string text;
      public:
      static const int max_field_size=100;

      explicit parser(const char *s) : text(s) {}

      void tokenize(char *fields[], int nf, const char breaker=' ') 
      {
           // parses internal string, breaking at instances of <breaker>
           // which are thrown away. Returns separated values as fields
           std::istringstream buffer(text);
           int f=0;
           while (f<nf) 
           {
                 buffer.getline(fields[f], max_field_size, breaker);
                  ++f;
           }
      }
};
//end of parser class--------------------------------------


int main()
{
...
cin >> string s;
parsef(s);
...
}

//string parsing Function----------------------------------
void parsef(string s) 
{

const char* userstring="RUN filename and this\n";
char **fields = new char*[3];
for (int i=0; i<3; ++i)
fields[i]=new char[parser::max_field_size];

parser parse(userstring);
parse.tokenize(fields, 3);
for (int i=0; i<3; ++i)
cout <<  "word " << i << ": " << fields[i] << endl;

}
//end string parsing Function------------------------------



Only what I put in for 'userstring' is parsed. I don't know how to get the string I pass to the parse function to be the string parsed.

I thinking it's something simple, but I just can't figure it out. Could someone help, please? And thanks.
Last edited by admin II : 03-Sep-2007 at 07:28. Reason: changed [COLOR] to [CPP]
  #2  
Old 02-Sep-2007, 20:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with parsing a string.


Quote:
Originally Posted by baka101
I am trying to parse a user-entered string

If I am understanting:
1. You need the user to input a std::string

2. The string from part 1 is passed as an argument to parsef

3. The function parsef declares an object of the parser class and initializes it with whatever the user entered in part 1.

4. You use the parser function "tokenize" to break up the string

5. You print the results.

So, you need to perform steps 1 and 2 in your main() function.

The parsef function will be modified to use information that was fed to it to do the tokenizing.


I would suggest getline() to read a std::string from the user, since parsef needs a std::string as an argument.

So the main program could look like:

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

void parsef(string s);
int main()
{
    string myString;
    cout << "Enter a string: ";
    getline(cin, myString);
    parsef(myString);

    return 0;

}

Now parsef is going to use its "s" parameter rather than the hard-coded "userstring" shown by your example

CPP / C++ / C Code:
void parsef(string s)
{

    //const char *userstring = "RUN filename and this\n"; // just delete this
    char **fields = new char *[3];
    for (int i = 0; i < 3; ++i)
        fields[i] = new char[parser::max_field_size];

    // Then what?
    parser parse(???);
You need to declare a parser object and initialize it with the string, but there is no constructor that takes a std::string as an argument. The only constructor takes a const pointer to char. (It uses a so-called "C-style" string, not a std::string.) What to do?

Answer: The std::string member function c_str() is a const pointer to char and it points to a "C-style" string equivalent to the std::string.

CPP / C++ / C Code:
    parser parse(s.c_str());

From here on the operation is the same.

Regards,

Dave

Footnote: The parsef function allocates memory for three strings. It should de-allocate the memory before returning to the calling function. So at the end of parsef, I would expect to see something like:
CPP / C++ / C Code:
    for (int i = 0; i < 3; i++) {
        delete [] fields[i];
    }
    delete fields;

}
//end string parsing Function------------------------------


As far as that goes, I'm not sure why fields was dynamically allocated anyhow; why not just declare it as an array of three pointers to char? Then allocate the three char arrays. Or, maybe there is a reason? I mean, either way is "correct", I just generally avoid dynamic memory allocation unless there is a good reason. I'm funny that way.

As far as that goes, I'm not sure why tokenize is designed to take an array of pointers to char; why not an array of std::strings (or a std::vector of std::strings).
Oh, well...
  #3  
Old 03-Sep-2007, 13:00
baka101 baka101 is offline
New Member
 
Join Date: Sep 2007
Posts: 2
baka101 is on a distinguished road

Re: Help with parsing a string.


Thanks, I finally get it.
  #4  
Old 04-Sep-2007, 07:42
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Help with parsing a string.


Can u post the program here ?

I need our help.

I really do understand what u discussed here.

Thanks for your help.
  #5  
Old 04-Sep-2007, 08:10
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Help with parsing a string.


Can u post the program here ?

I need our help.

I really do understand what u discussed here.

Thanks for your help.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
C++ String Template Parsing vikasbucha C++ Forum 1 11-Jun-2006 03:14
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 01:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.