Config string parse to select user-entered strings.

Discussion in 'C' started by bakatu, Sep 3, 2007.

  1. bakatu

    bakatu New Member

    Joined:
    Sep 3, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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:
    Code:
    //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.
     
    Last edited by a moderator: Sep 3, 2007
  2. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    it might be easier to describe the inuts you are getting and what you want the outputs to be
     
  3. bakatu

    bakatu New Member

    Joined:
    Sep 3, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Like the old code says, the input is: const char* userstring="RUN filename and this\n";
    which is only "RUN filename and this".

    The output of that is:
    RUN
    filename
    and
    this

    I want to enter my own string, not what already is in the code, and have that parsed at the spaces.

    Ex:
    when prompted for a string when the program is running, I type:
    "Hello I typed this"

    Output:
    Hello
    I
    typed
    this

    I hope that clears that up. :)

    -------------------------------------------------
    Updated: I have most of the parser figured out, but I am getting an error on this line:

    std::istringstream buffer(text);

    error: variable `std::istringstream buffer' has initializer but incomplete type

    I don't know what' wrong with the type, so could someone help me with that?


    Here's the new code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //---------------------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
    
    [COLOR=DarkOliveGreen]//this is the error[/COLOR]
               std::istringstream buffer(text);
    [COLOR=DarkOliveGreen]//this is the error[/COLOR]
    
               int f=0;
               while (f<nf) 
               {
                     buffer.getline(fields[f], max_field_size, breaker);
                      ++f;
               }
          }
    };
    //---------------------end of parser class--------------------------------------
    
    void parsef(string s);
    
    int main()
    {
       string myString;
       cout << "Enter a string: ";
       getline(cin, myString);
       parsef(myString);
    
       return 0;
    
    }
    
    
    void parsef(string s)
    {
    
       
       char **fields = new char *[3];
       for (int i = 0; i < 3; ++i)
           fields[i] = new char[parser::max_field_size];
           parser parse(s.c_str());
               for (int i = 0; i < 3; i++) {
            delete [] fields[i];
        }
        delete fields;
    
    }
    //end string parsing Function------------------------------
     
    Last edited by a moderator: Sep 4, 2007
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Put your code in code tags. If you don't know what that means, read the "Before you make a query" thread. Also, some Visine would be good to get the red out.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,345
    Likes Received:
    382
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice