Validating integers separated by white C ++

I saw other SO questions regarding input format validation, however I am looking for a more specific answer to the question I have.

Currently, I need to read 2 integer values ​​and save them as coordinates. I used:

cin >> x >> y

which will read the x and y values, separated by a space.

However, any input other than

int whitepsace int

will destroy my program. I tried to check the input to ensure that each value is an integer, however it only applies to the first integer input.

I also need to make sure that the input format remains as int whitespace int, so that I can consistently use the values ​​in my program.

Any ideas?

Things I tried with no luck:

  • do - while w/ cin.fail() max().
  • isDigit()
+4
1

std::string. , std::cin .

, a std::istringstream, operator>>, int. fail(), , , std::istringstream .

std::istringstream , get() eof, int.

.

, , , , , , ++, .

+3

Source: https://habr.com/ru/post/1652398/


All Articles