question on character arrays
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
#include <sstream> char data[] = "12-06 89"; std::istringstream iss(data); int num1, num2, num3; iss >> num1; iss.ignore(1); // Skip '-' iss >> num2 >> num3;
wow, thanks a ton, I diddnt expect to have to use anything outside of iostream, but this works great!
thanks again!
thanks again!
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)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement