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_ty pe 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_fir st_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 4 4037
"Christophe r" <an**@nospam.ne t> 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_ty pe 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
changed it to getline(cin,buf fer) 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... "Christophe r" <an**@nospam.ne t> 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_ty pe 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
"Christophe r" <an**@nospam.ne t> wrote in message
news:JM******** ***********@fe1 .texas.rr.com.. . changed it to getline(cin,buf fer) 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_l imits<std::stre amsize>::max(), '\n');
Read this FAQ - http://www.parashift.com/c++-faq-lit....html#faq-15.2
-Sharad
"Sharad Kala" <no************ *****@yahoo.com > wrote in message news:<c6******* *****@ID-221354.news.uni-berlin.de>... "Christophe r" <an**@nospam.ne t> 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_ty pe 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::istringstr eam;
// 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_ba ck(dummy);
++argcnt;
input >> dummy;
}
HTH, Dave Moore This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 effectively impossible to forward declare
std::string. (Yes I am aware that some libraries have a string_fwd.h header,
but this is not portable.)
That said, is there any real reason why I can't derive an otherwise empty
|
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( (idx=str.find_first_of(',')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}
|
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 description of how each works.
I've been trying Google for the last 20 minutes but can't
get anything decent.
Thanks.
|
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;
char c;
std::string str;
|
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 found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these...
| |
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 a namespace and another
typedef to basic_string:
namespace my_string
{
|
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 example
string str1; as a class member and then add text to it. or do I have
to
specify it's length when defining?
2. How to convert from std::string to LPCSTR
|
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;
|
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 ""
// 'words' are any sequences of non-whitespace characters
// leading, trailing and multiple whitespace characters
// should be ignored.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |