473,396 Members | 1,599 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,396 software developers and data experts.

Contolling istringstream input


I want to get from stdin a string that might look like any of these three
options

99
99lb
99 lb
but if a number isn't at the beginning for the string, or is the left over
letters aren't either lb or kg, for an error to be be throw.

I can't seem to find a reasonable means of doing this.

If can do a
cin >some_previous_double;
buf[] = 512;
istringstream stream1;
cin.ignore();
cin.getline(buf, 512);
try{
stream1.str(descr);
if (!(stream1>>hgt_))
{
errormsg.append(descr);
throw errormsg;
}
if(!(stream1>>unit_))
{
errormsg.append(descr);
throw errormsg;
}
if( unit_ == "meters"|unit_ == "meter" || unit_ == "m" \
|| unit_ == "mt"|| unit_ == "mtr"|| unit_ == "mt")
{
unit_ = "in";
hgt_ = hgt_ * 39.37;

}else if( unit_ == "in" || unit_ == "inch" || unit_ == "i"\
|| unit_ =="inches" ){
unit_ = "in";
}else{
errormsg.append(":You entered =>");
errormsg.append(descr);
throw errormsg;
}
}
catch (string errormsg){
cout << "Error: Units error - can\'t determine type\n" << errormsg;
cout << "\n" << "Setting Height to 68 inches\n";
hgt_ = 68;
unit_ = "in";
}


--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 16 '08 #1
1 1253
On Sep 17, 1:03 am, Ruben <ru...@www2.mrbrklyn.comwrote:
I want to get from stdin a string that might look like any of
these three options
99
99lb
99 lb
but if a number isn't at the beginning for the string, or is
the left over letters aren't either lb or kg, for an error to
be be throw.
I can't seem to find a reasonable means of doing this.
boost::regex matcher( "(\\d+)(?:[[:blank:]]*(lb|kg))?$" ) ;

The first capture contains the number, the second is either
empty, or contains the "lb" or the "kg".

(BTW: your signature is incorrectly delimited, and is longer
than what is generally considered acceptable. The delimiter
should be a line containing exactly "-- ", and nothing else, and
four lines is generally considered the maximum acceptable.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 17 '08 #2

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

Similar topics

1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
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...
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.