Wednesday, December 27, 2017

C++98: cin.ignore()

The cin.ignore() function allows for a sequence of input characters to be ignored. Here is an example:

#include <iostream>

int main()
{
  int myInt1 = 0;
  int myInt2 = 0;

  std::cin >> myInt1; // 1

  std::cin.ignore(256, '\n'); // Extracts and discards characters

  std::cin >> myInt2; // 3

  std::cout << myInt1 << " "; // 1
  std::cout << myInt2 << " "; // 3

  std::cout << std::endl;

  return 0;
}
// Input: 
//   1 2
//   3 4
// Output:
//   1 3
Reference: http://www.cplusplus.com/reference/istream/istream/ignore/

No comments:

Post a Comment