473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

std::string.find_first_of()

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

Jul 22 '05 #1
4 4010

"Christopher" <an**@nospam.net> wrote in message
news:yq*******************@fe1.texas.rr.com...
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
Jul 22 '05 #2
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" <no*****************@yahoo.com> wrote in message
news:c6************@ID-221354.news.uni-berlin.de...

"Christopher" <an**@nospam.net> wrote in message
news:yq*******************@fe1.texas.rr.com...
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

Jul 22 '05 #3

"Christopher" <an**@nospam.net> wrote in message
news:JM*******************@fe1.texas.rr.com...
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::streamsiz e>::max(), '\n');
Read this FAQ - http://www.parashift.com/c++-faq-lit....html#faq-15.2

-Sharad
Jul 22 '05 #4
"Sharad Kala" <no*****************@yahoo.com> wrote in message news:<c6************@ID-221354.news.uni-berlin.de>...
"Christopher" <an**@nospam.net> wrote in message
news:yq*******************@fe1.texas.rr.com...
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.