Contolling istringstream input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ruben

    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(des cr);
    if (!(stream1>>hgt _))
    {
    errormsg.append (descr);
    throw errormsg;
    }
    if(!(stream1>>u nit_))
    {
    errormsg.append (descr);
    throw errormsg;
    }
    if( unit_ == "meters"|un it_ == "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

  • James Kanze

    #2
    Re: Contolling istringstream input

    On Sep 17, 1:03 am, Ruben <ru...@www2.mrb rklyn.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:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    Working...