 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Mantoulidis Guest
|
Posted: Fri Feb 27, 2004 4:20 pm Post subject: Weird problem with std::string |
|
|
PROBLEM: I'm having some weird problems with string::find() (in
ParamGenerate()), but since I'm not sure if that is the source of all
bad output in my program, I'm posting least code that's compilable.
OTHER INFO: Just so you know what the program does (and so you can
give it some input to test it), at this stage I want it to break the
input into some parts. Here is the input style:
<middle param> <middle param> ...... <middle param> (<final param>)
There can be infinite <middle param>s but ONLY ONE (and optional)
<final param>. Each middle param is seperated from the others with a
space and cannot contain a column (':'). The final param is just one
and starts with a ':' and can contain any kind of character.
- MORE INFO
- here's a sample command (the column is included in the beginning)
- because a column is the start of an IRC command
- :cmad!cmad_x@host PRIVMSG #cmad :this is a test message
-
- I would like this text broken into those parts
- params[0] = "cmad!cmad_x@host"
- params[1] = "PRIVMSG"
- params[2] = "#cmad"
- params[3] = ":this is a test message"
-
- but this is not the output I get
(BTW: If I try:
params.resize(ParamCount(s));
in ParamGenerate() I get a SegFault, so I set the vector size to 1024)
Here's the program code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string arg;
vector<string> params(1024);
/*
COMMAND IS LIKE
:user@host <CMD> <PARAMS>
and params are like
<PARAMS> = <MIDDLE PARAMS> <MIDDLE PARAMS> ... <MIDDLE PARAMS> <FINAL
PARAM>
middle params are params that contain no spaces or columns ':'
.... TO BE CONTINUED ...
*/
string RemoveLeadingColumn(const string &s) {
string s2 = s;
if (s2[0] == ':') s2.erase(0,1);
return s2;
}
int ParamCount(const string &s) {
int count=1;
string::size_type i;
for (i=0; i < s.length(); i++) {
if (s[i] == ' ' /*&& !found_column*/ )
count++;
else if (s[i] == ':' /*&& !found_column*/ ) {
return count;
}
}
return count;
}
void ParamGenerate(const string &s) {
cout << "initializing vars...n";
string::size_type first_column = s.find(':');
if (first_column == string::npos) first_column=s.length();
string::size_type white_space=0;
string::size_type last=0;
int curr_param=0;
cout << "starting search...n";
while ((white_space = s.find(' ', white_space+1)) != string::npos &&
white_space <= first_column) {
cout << "found space...n";
params[curr_param] = s.substr(last,white_space);
cout << "substring is "" << s.substr(last,white_space) << ""n";
last=white_space+1;
curr_param++;
}
params[curr_param] = s.substr(first_column,s.length());
}
int main() {
cout << "gimme a cmd:n";
getline(cin,arg,'n');
cout << "you gave me: " << arg << "n";
cout << "without leading ':' it is: " << RemoveLeadingColumn(arg) <<
"n";
if (IsAddr(RemoveLeadingColumn(arg)))
cout << "nickname: " << NickFromAddr(RemoveLeadingColumn(arg)) <<
"n";
cout << "ParamCount: " << ParamCount(RemoveLeadingColumn(arg)) <<
"n";
ParamGenerate(RemoveLeadingColumn(arg));
cout << "Params generatedn";
int what;
do {
cout << "tell me a param num: ";
cin >> what;
if (what <= ParamCount(RemoveLeadingColumn(arg)) && what >= 1)
cout << "param[" << what << "] = " << params[what-1] << "n";
else
cout << "wrong indexn";
} while (what != 0);
return 0;
}
|
|
| Back to top |
|
 |
Heinz Ozwirk Guest
|
Posted: Fri Feb 27, 2004 8:14 pm Post subject: Re: Weird problem with std::string |
|
|
"Chris Mantoulidis" <cmad_x (AT) yahoo (DOT) com> schrieb im Newsbeitrag news:a8587dd9.0402270820.35a5ed5d (AT) posting (DOT) google.com...
: PROBLEM: I'm having some weird problems with string::find() (in
: ParamGenerate()), but since I'm not sure if that is the source of all
: bad output in my program, I'm posting least code that's compilable.
Without IsAddr and NickFromAddr this code doesn't compile, but that doesn't really matter.
: string RemoveLeadingColumn(const string &s) {
: string s2 = s;
: if (s2[0] == ':') s2.erase(0,1);
: return s2;
: }
This function removes a leading colon, not a column. This doesn't matter neither, but the name is really misleading a reader.
: void ParamGenerate(const string &s) {
You should pass the destination for the generated parameter list, too. In general, global variables are not a good idea. Don't get used to them.
: cout << "initializing vars...n";
: string::size_type first_column = s.find(':');
: if (first_column == string::npos) first_column=s.length();
: string::size_type white_space=0;
: string::size_type last=0;
: int curr_param=0;
: cout << "starting search...n";
: while ((white_space = s.find(' ', white_space+1)) != string::npos &&
: white_space <= first_column) {
: cout << "found space...n";
: params[curr_param] = s.substr(last,white_space);
The second argument to std::string::substr is the length of the requested substring, not the position of the first character beyond the end of the string. So replace substr(a,b) by substr(a,b-a).
: cout << "substring is "" << s.substr(last,white_space) << ""n";
: last=white_space+1;
: curr_param++;
: }
: params[curr_param] = s.substr(first_column,s.length());
: }
:
: int main() {
:
: cout << "gimme a cmd:n";
: getline(cin,arg,'n');
:
: cout << "you gave me: " << arg << "n";
:
: cout << "without leading ':' it is: " << RemoveLeadingColumn(arg) <<
: "n";
:
:
: if (IsAddr(RemoveLeadingColumn(arg)))
Why do you call RemoveLeadingColumn over and over again. Why don't you just remove any leading colon and save the resulting string for later use? And why do you remove a leading colon at all. Your specification does not mention it.
: cout << "nickname: " << NickFromAddr(RemoveLeadingColumn(arg)) <<
: "n";
:
: cout << "ParamCount: " << ParamCount(RemoveLeadingColumn(arg)) <<
: "n";
:
: ParamGenerate(RemoveLeadingColumn(arg));
: cout << "Params generatedn";
:
: int what;
: do {
: cout << "tell me a param num: ";
: cin >> what;
: if (what <= ParamCount(RemoveLeadingColumn(arg)) && what >= 1)
params.size() would be much faster than ParamCount(RemoveLeadingColumn(arg))
: cout << "param[" << what << "] = " << params[what-1] << "n";
: else
: cout << "wrong indexn";
0 may be a bad input, but since it is the command to stop this loop, you shouldn't print an error message.
: } while (what != 0);
:
: return 0;
:
: }
Heinz
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|