Tricky Tricks ›› Programming Tricks ›› C Languages Tricks ›› Leading Whitespace (Carriage Returns, Tabs, Spaces) Is Ignored By Cin

Leading Whitespace (Carriage Returns, Tabs, Spaces) Is Ignored By Cin

C++ Programming Tricks

By default, leading whitespace (carriage returns, tabs, spaces) is ignored by cin:

 Given: 
 
 int i;
 float fl;
 std::cin >> fl;
 std::cin >> i;
 And you type: 3.1442 
 

3.14 is read into fl . The carriage return (newline) following the 3.14 is still sitting on the input buffer.

Since std::cin ignores whitespace, the first return is "eaten" by std::cin >> i . Then the integer 42 is read into i and the second return is left on the input buffer.

Partners