BlockLune's Blog

Home Tags About |

Use std::cin.ignore() to keep the user typing until the input meets the requirements

Published at 2023-03-25
Last update over 365 days ago Licensed under CC BY-NC-SA 4.0 cppcinput-validationstd-cin

I used to control users’ input by making the input a string and process it later. But today, my friend posed this question to me: Can we control input without a char array or string class?

On cppreference.com, I found std::cin.ignore(). This function can help us solve the previous question. Here is an example:

#include <iostream>
#include <limits>

int main() {
  int n = 0;
  while (true) {
    std::cout << "Please input a number: ";
    std::cin >> n;
    if (!std::cin.good()) {
      std::cerr << "Error input." << std::endl;
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      continue;
    } else {
      break;
    }
  }
  std::cout << "Your input is: " << n << std::endl;
  return 0;
}