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