Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Configuring a string-parse to select user-ntered strings.

string

  • Please log in to reply
3 replies to this topic

#1 bakatu

bakatu

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 02 September 2007 - 04:18 PM

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:
//parser 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 to do, but I just can't figure it out.
Could someone help, please? And thanks.
  • 0

#2 Crane

Crane

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 388 posts

Posted 02 September 2007 - 05:03 PM

I think I understand what you want but am not sure. You want to parse what the user inputs by cin << s, correct?

If so, change
parser parse(userstring);

to

parser parse(s);

You may have to change the input from a string to a char before you send it through your parser though.
  • 0

#3 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 02 September 2007 - 09:15 PM

There's easier ways to parse a string. This is a good link, showing how to easily split a string. You could, instead of showing the splitted string, like in the link, throw all the pieces into a vector. Then the arguments for your command would be saved in respectively order, ready to take out, and use.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 bakatu

bakatu

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 03 September 2007 - 12:10 PM

Thanks, that helped a lot!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download