question on character arrays

Started by
3 comments, last by Aprosenf 19 years, 4 months ago
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
01001001001000000111010001101000011010010110111001101011001000000111100101101111011101010010000001110111011010010110110001101100001000000110011001101001011011100110010000101100001000000111010001101000011000010111010000100000011110010110111101110101001000000110000101110010011001010010000001101101011001010111001001100101011011000111100100100000011010010110111001110011011010010110010001100101001000000110000100100000011010100110010101101100011011000111100100101110
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;
wow, thanks a ton, I diddnt expect to have to use anything outside of iostream, but this works great!
thanks again!
01001001001000000111010001101000011010010110111001101011001000000111100101101111011101010010000001110111011010010110110001101100001000000110011001101001011011100110010000101100001000000111010001101000011000010111010000100000011110010110111101110101001000000110000101110010011001010010000001101101011001010111001001100101011011000111100100100000011010010110111001110011011010010110010001100101001000000110000100100000011010100110010101101100011011000111100100101110
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