• Advertisement
Sign in to follow this  

question on character arrays

This topic is 4797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a bit of a problem here. What I need to do is turn a character array storing numbers and characters (such as an array containing "12-06 89") into an integer, but only for the first 2 characters. so out of the character array "12-06 89" I want integer m to store the first two characters as an integer value of 12. Is this possible? by the way, I am using C++. Thank you, Justin

Share this post


Link to post
Share on other sites
Advertisement
#include <sstream>

char data[] = "12-06 89";

std::istringstream iss(data);

int num1, num2, num3;

iss >> num1;

iss.ignore(1); // Skip '-'

iss >> num2 >> num3;

Share this post


Link to post
Share on other sites
wow, thanks a ton, I diddnt expect to have to use anything outside of iostream, but this works great!
thanks again!

Share this post


Link to post
Share on other sites
that ignore(); isn't even necessary, integer reading is stopped when a non integer character is encountered (in fact all non-number characters are non-int, but with float, if there is a '.' character, the float value is correctly read)

Share this post


Link to post
Share on other sites
That's true, but the delimiting character is not discarded. If you did not call ignore(), the next number read in would be -6, and I'm assuming that Whelzorn wants it to be +6, not -6.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement