C++ cin.ignore

2021. 3. 11. 16:35·C++/Library

입력 버퍼 무시하기

  • cin.ignore(_Count, _Metadelim)

  • _Count : 무시할 문자의 최대 개수(바이트)

    • 기본 값은 1

    • 정석대로라면 <limits> 라이브러리의 std::numeric_limits<std::streamsize>::max()를 사용하는게 맞으나... 귀찮으므로 보통 적당히 큰 수를 채택하는 것 같다.

  • _Metadelim : 이 문자가 나올 때까지 무시한다.(해당 문자 포함)

    • 기본 값은 eof
#include <iostream>

int     getInt()
{
  while (true)
  {
    int x;

    std::cout << "Enter an integer number : ";
    std::cin >> x;
    if (std::cin.fail())
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      std::cout << "Invalid number, please try again\n";
    }
    else
    {
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      return (x);
    }
  }
}

char    getOperator()
{
  while (true)
  {
    char op;

    std::cout << "Enter an operator (+, -) : ";
    std::cin >> op;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    if (op == '+' || op == '-')
      return (op);
    else
      std::cout << "Invalid Operator, please try again" << std::endl;
  }
}

void    printResult(int a, char op, int b)
{
  if (op == '+')      std::cout << a + b << std::endl;
  else if (op == '-') std::cout << a - b << std::endl;
  else                std::cout << "Invalid operator" << std::endl;
}

int     main()
{
  using namespace std;

  int     a = getInt();
  char    op = getOperator();
  int     b = getInt();

  printResult(a, op, b);
}
저작자표시

'C++ > Library' 카테고리의 다른 글

C++ array  (0) 2021.03.11
C++ typeinfo  (0) 2021.03.11
C++ cin  (0) 2021.03.11
C++ 난수 생성 (Random Number Generation)  (0) 2021.03.11
C++ 입력 버퍼 무시하기  (0) 2021.03.10
'C++/Library' 카테고리의 다른 글
  • C++ array
  • C++ typeinfo
  • C++ cin
  • C++ 난수 생성 (Random Number Generation)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317) N
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20) N
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    맥북 카카오톡 알림 안뜸
    윈도우
    백기선
    Solaris 10
    KakaoTalk
    Workspace
    EXCLUDE
    SFC
    스프링 프레임워크 핵심 기술
    SunOS 5.1
    unix
    MacOS
    윈도우 명령어
    Windows 11
    그림판
    mspaint
    spring
    시스템 복구
    vscode
    알림
    제외
    citrix workspace
    java
    windows
    로지텍 마우스 제스처
    logi options
    dism
    스프링
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ cin.ignore
상단으로

티스토리툴바