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
//this is the error
std::istringstream buffer(text);
//this is the error
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------------------------------