Taking in a double - space between symbol and number.

Hello, while taking a number to double,
1
2
double a;
std::cin >> a;

If I type in -1 , a gets the value -1.
But if I type - 1 , a gets the value 0. (Notice space between - and 1).
I know how to fix this by accepting a char, check if number or symbol, ignore spaces and then take in the number, but is there a more elegant way to fix this?
Last edited on
Hi,

Can you read the input as a string, then remove spaces, then convert the value using stringstream? http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

I would avoid reading char by char, there are various formats for double, might as well get the system to work for you.

Edit:
If you are aware of a particular format, you can use stringstream to process that format.

Someone else is bound to have an even better way ..... :+)
Last edited on
Thanks! I will try string stream!
Last edited on
Topic archived. No new replies allowed.