참조 : "전문가를 위한 C++ 개정 5판"

유용한 매니퓰레이터

  • <ios> 또는 <iomanip> 표준 헤더에 정의
  • 유용하게 쓰이는 것들
    • boolalpha, noboolalpha : 스트림에 bool 값을 true, false로 출력
    • hex, oct, dec : 숫자를 각 해당 진수로 표현
    • setprecision : 분숫값을 표현할 때, 사용할 소수점 자릿수 지정; 인수 받음
    • setw : 데이터 출력할 필드 너비 지정; 인수 받음; 다음 출력에만 적용
    • setfill : 지정 너비보다 작을 때, 빈 공간 채울 문자 지정; 인수 받음
    • showpoint, noshowpoint : 부동 소수점에서 소수점 부분 없을 떄, 소수점 표현할 지 지정;

입력 에러처리

  • 표준 입력 에러처리 방식 예시

      if (!cin.good())
      {
          cerr << "Standard input is in a bad state!" << endl;
      }
      int sum { 0 };
      while (!cin.bad())
      {
          int number;
          cin >> number;
          if (cin.good())
          {
              sum += number;
          }
          else if (cin.eof())
          {
              break; // 파일의 끝 도달
          }
          else if (cin.fail())
          {
              cin.clear() // 에러상태 제거
              string badToken;
              cin >> badToken;
              cerr << "WARNING: Bad Input encounterd: " << badToken << endl;
          }
      }
    
      cout << "The Sum is " << sum << endl;
      /*
      1
      2
      test
      WARNING: Bad Input encounterd: test
      3
      The Sum is 6
      */
  • 이름 입력 받기 예시

      string readName(istream& stream)
      {
          string name;
          char next;
          while(stream.get(next))
          {
              name += next;
          }
          return name;
      }

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

Attribute  (0) 2025.03.27
C++ IO스트림  (0) 2025.03.27
decltype  (0) 2025.03.26
추상 클래스  (0) 2025.03.15
문자열  (1) 2025.03.14

+ Recent posts

let textNodes = document.querySelectorAll("div.tt_article_useless_p_margin.contents_style > *:not(figure):not(pre)"); textNodes.forEach(function(a) { a.innerHTML = a.innerHTML.replace(/`(.*?)`/g, '$1'); });