FlappyBoy123
FlappyBoy123

Reputation: 31

C++ comma weird behavior with numbers

I have a simplest possible code which has to accept and reprint two numbers. It works well for any separating symbol I've tried (space, + ,etc.) except (!) the comma.

cout << "Enter x & y coordinates: ";
cin >> x;
cin >> y;
cout << "Coordinates are (" << x << "," << y << ")" << endl;

but 2,2 doesn't prompt for a second number and just outputs (2,0). Why is this happening? Where does the stuff after the comma go?

Upvotes: 1

Views: 88

Answers (1)

Jarod42
Jarod42

Reputation: 218268

It works well for any separating symbol I've tried (space, + ,etc.) except (!) the comma.

In fact, no.

  • spaces (including tab, eol) are indeed ignored.
  • 2+2 are 2 numbers: 2 and +2 (as number can begin with + or -)

In other cases, std::cin >> y would fail, and set cin in error state, and set y to 0.

Upvotes: 3

Related Questions