Skip to content

Instantly share code, notes, and snippets.

@akaz00
Created September 6, 2016 01:08
Show Gist options
  • Select an option

  • Save akaz00/b67f9a59ff8511df6942a3563711e2c1 to your computer and use it in GitHub Desktop.

Select an option

Save akaz00/b67f9a59ff8511df6942a3563711e2c1 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
// 사이즈 5이지만 4개만 저장되는 이유!? -> null terminator 저장 때문
// 따라서 저장할 문자가 n개면 사이즈는 n+1로 잡기
char *buf = new char[6];
cin.get(buf, 6);
cin.get(); // 개행 문자 잡아먹기 (안그러면 뒤의 cin이 제대로 작동 X)
cout << buf << endl;
cin.getline(buf, 6); // 여기서는 따로 개행 문자 잡아먹을 이유 X, 14번 라인 지우고 맨 위에 abcdef 입력해보기
// 다섯자까지 입력하고 남은 f와 개행(\n)
// 만약 cin.get();이 있으면 14번 라인에서 f를 처리하고 개행 문자를 getline에서 입력받음
// 만약 cin.get();이 없으면 14번 라인에서 f를 처리하지 않으므로 getline에서 f를 받고 엔터를 처리받아서 바로 f가 출력됨
cout << buf << endl;
cin.getline(buf, 6);
cout << buf << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment