hey guys have question storing output variable. let's have 3 variables defined follows:
float num, string units, string rest;
and user enters in console:
12.2 mg vitamin
i know if want store in variables, have following:
cin >> num >> units >> rest;
but let's user enters vitamin instead of vitamin.
and want store rest of string after mg 'rest' variable. how do that?
i did following:
cin >> num >> units; getline(cin,rest); //stores rest of string rest
but stores space character after mg
namely if output rest
, output " vitamin a"
. don't want space in beginning. how accomplish this?
i know it's long hope made clear. or suggestions helpful. thanks,
you can skip whitespace using std::ws
cin >> num >> units; ws(cin); getline(cin, rest);
you can see live working example here: ignore leading whitespace
Comments
Post a Comment