c++ extracting double from stream

前端 未结 3 1278
一整个雨季
一整个雨季 2021-01-14 05:35

I\'ve a funny problem with school exercise. I am given latitude and longitude and I have to ensure that it is in right format: \\(\\d+\\.\\d+[NS], \\d\\+.\\d+[EW]\\)

3条回答
  •  梦毁少年i
    2021-01-14 06:24

    You're essentially looking at a parse problem. operator>>(istream&, string&) is a very simplistic parser that just tokenizes on whitespace.

    If your format specification is strict enough, and you should reject (51.5N , 0.0E) (extra space before comma) then just don't extract the comma. Instead, directly after extraction you should check that nLat contains the trailing comma and remove it. You no longer need >>comma.

    If you must support optional whitespace anywhere, it may help to preprocess the string by inserting an extra space before and after the comma (unconditionally). If there already was a space, there will now be two. That's no problem as whitespace skipping will skip any amount.

提交回复
热议问题