![]() |
|
#1
|
|||
|
|||
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:
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
|
|||
|
|||
Re: Help with parsing a string.Quote:
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:
Now parsef is going to use its "s" parameter rather than the hard-coded "userstring" shown by your example CPP / C++ / C Code:
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:
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:
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
|
|||
|
|||
Re: Help with parsing a string.Thanks, I finally get it.
|
|
#4
|
|||
|
|||
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
|
|||
|
|||
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 GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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