Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualTrienco

Posted 28 December 2012 - 11:38 PM

If you can rely on your input being formatted like that, just dump it in a stringstream and read it back.
 

const string res = "640x480x32";
int height, width, depth;
char delimiter;  


stringstream stream(res);
stream >> height >> delimiter >> width >> delimiter >> depth;

 

 

You could use getline and define 'x' as a delimiter, but then you get each number as a string and still need to convert to int.

You could use .ignore(1) instead of the dummy character, but it will look horrible to read.


#1Trienco

Posted 28 December 2012 - 11:37 PM

If you can rely on your input being formatted like that, just dump it in a stringstream and read it back.

 

 
</p><div>const string res = "640x480x32";</div>
<div>
<div>int height,width,depth;</div>
<div>char delimiter;</div>
<div> </div>
</div>
<div> </div>
<div>stringstream stream(res);</div>
<div> </div>
<div>stream >> height >> delimiter >> width >> delimiter >> depth;</div>
<div>
 
You could use getline and define 'x' as a delimiter, but then you get each number as a string and still need to convert to int.
 
You could use .ignore(1) instead of the dummy character, but it will look horrible to read.

PARTNERS