std::string.find_first_of()

C

Christopher

I am using std::string to parse a command given by the user, I don't
understand why the following snippet is not working as expected.

string buffer, // store commands from user
command,
argument;
string::size_type index = string::npos;

cout<<"\nThe Following are valid commands:\n";
cout<<"read_all <number> -- display entire mail header and body.\n";
cout<<"read <number> -- display body of message\n";
cout<<"del <number> -- delete message\n";
cout<<"undel -- undo all deletion\n";
cout<<"reply <number> -- reply to message\n";
cout<<"list <number> -- display the next 10 message summaries\n";
cout<<"quit -- exit this program\n\n";

cin>>buffer;

index = buffer.find_first_of(' ');

if(index != string::npos )
{
cout<<"first\n";
command = buffer.substr(0, index);
buffer.erase(0, index);
argument = buffer;
}
else
{
cout<<"second\n";
command = buffer;
argument = "";
}

cout<<index<<"\n";
cout<<command<<" "<<argument<<"\n


my input:
hello there

output:
second
4294967295
helllo

expected output:
first
6
hello there

Thanx,
Chris
 
S

Sharad Kala

Christopher said:
I am using std::string to parse a command given by the user, I don't
understand why the following snippet is not working as expected.

string buffer, // store commands from user
command,
argument;
string::size_type index = string::npos;
[snip]
cin>>buffer;
The whitespace in "Hello there" makes buffer to contain "Hello" and not "Hello
there".

Change it to -
getline(cin, buffer);

-Sharad
 
C

Christopher

changed it to getline(cin,buffer) and now it just skips over it without
waiting for input from the user. Maybe something is already in the instream?
if so how can I make sure it is empty and only filled with what the user
enters at that point?

Thanks,
Chris

Sharad Kala said:
Christopher said:
I am using std::string to parse a command given by the user, I don't
understand why the following snippet is not working as expected.

string buffer, // store commands from user
command,
argument;
string::size_type index = string::npos;
[snip]
cin>>buffer;
The whitespace in "Hello there" makes buffer to contain "Hello" and not "Hello
there".

Change it to -
getline(cin, buffer);

-Sharad
 
S

Sharad Kala

Christopher said:
changed it to getline(cin,buffer) and now it just skips over it without
waiting for input from the user. Maybe something is already in the instream?
if so how can I make sure it is empty and only filled with what the user
enters at that point?
Please don't top-post.
Well the error bit in input stream is sticky, you need to clear it explicitly.
std::cin.clear();
To skip invalid input characters -
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Read this FAQ - http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2

-Sharad
 
D

Dave Moore

Sharad Kala said:
Christopher said:
I am using std::string to parse a command given by the user, I don't
understand why the following snippet is not working as expected.

string buffer, // store commands from user
command,
argument;
string::size_type index = string::npos;
[snip]
cin>>buffer;
The whitespace in "Hello there" makes buffer to contain "Hello" and not "Hello
there".

Change it to -
getline(cin, buffer);

-Sharad


There is also an (IMHO) easier way to do this in general using
stringstreams;

#include <sstream>
using std::istringstream;

// using your declarations

if (!getline(cin, buffer)) throw "I/O error"; // just as a simple
example
istringstream input(buffer);
input >> command;
if (!input) throw "No input on stream"; // just as a simple example
input >> argument;
if (!input) {
// handle case for no argument
} else {
// handle case for an argument present
}

I find this a more natural way to deal with formatted input ..
especially when reading from a text file. It also extends well to the
case where you don't know ahead of time how many arguments to expect
in the input stream. For example:

#include <deque>
std::deque<std::string> arglist;
int argcnt=0;

string dummy;
input >> dummy;
while (input) {
arglist.push_back(dummy);
++argcnt;
input >> dummy;
}

HTH, Dave Moore
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top