C++

C++에서 cin 총정리

김구티2 2024. 3. 9. 12:54

1. cin의 개념

C++에서 cin 객체는 class iostream의 객체다. 이것은 표준 입력 장치로, 키보드로부터 입력을 받아들이는 데 사용된다. 그래서 표준 C 입력인 stream stdin과도 연관되어 있다. 추출 연산자(>>)가 입력을 읽기 위한 객체 cin과 함께 사용된다. 추출 연산자는 키보드를 사용하여 입력된 객체 cin에서 데이터를 추출한다.

 

예시1:

// C++ program to demonstrate the 
// cin object 
#include <iostream> 
using namespace std; 

// Driver Code 
int main() 

     string s; 

     // Take input using cin 
     cin >> s; 

     // Print output 
     cout << s; 

     return 0; 
}

 

이것을 실행하면, 결과에 입력을 할 수 있는 창이 뜨고, 거기서 입력을 하면 그것이 그대로 출력으로 나오는 것이다.

 

예시2:

// C++ program to illustrate the take 
// multiple input 
#include <iostream> 
using namespace std; 

// Driver Code 
int main() 

     string name; 
     int age; 

     // Take multiple input using cin 
     cin >> name >> age; 

     // Print output 
     cout << "Name : " << name << endl; 
     cout << "Age : " << age << endl; 

     return 0; 
}

 

INPUT(위의 예시와 마찬가지로 내가 입력한다.):

Guti

5

 

출력:

Name : Guti

Age : 5

 

2.  cin의 다양한 종류

이제 여기서 추가로 cin.getline(char *buffer, int N)에 대해 알아보도록 한다. 길이가 N인 문자의 스트림을 문자열 버퍼로 읽으며, N-1개의 문자를 읽었거나, 파일의 끝이나 새 줄 문자(\n)를 찾으면 중지된다.

 

예시:

// C++ program to illustrate the use 
// of cin.getline 
#include <iostream> 
using namespace std; 

// Driver Code 
int main() 

     char name[5]; 

     // Reads stream of 3 
     // characters 
     cin.getline(name, 3); 

     // Print output 
     cout << name << endl; 

     return 0; 
}

 

INPUT:

Guti

 

출력:

Gu

 

이번엔 cin.get(char& var)에 대해 알아보도록 한다. 이것은 입력된 문자를 읽어 변수에 저장한다.

 

예시:

// C++ program to illustrate the use 
// of cin.get() 
#include <iostream> 
using namespace std; 

// Driver Code 
int main() 

     char ch; 
     cin.get(ch, 25); 

     // Print ch 
     cout << ch; 
}

 

INPUT:

Welcome to Gutilog

 

출력:

Welcome to Gutilog

 

이번엔 cin.read(char *buffer, int N)에 대해 알아보도록 한다. 이것은 길이가 N인 문자 스트림을 읽는다.

 

예시:

// C++ program to illustrate the use 
// of cin.read() 
#include <iostream> 
using namespace std; 

// Driver Code 
int main() 

     char gfg[20]; 

     // Reads stream of characters 
     cin.read(gfg, 10); 

     // Print output 
     cout << gfg << endl; 

     return 0; 
}

 

INPUT:

Welcome to Gutilog

 

출력:

Welcome to

 

이번엔 cin.ignore()에 대해 알아보도록 한다. 이것은 입력 버퍼에서 하나 이상의 문자를 무시하거나 지운다.

 

예시:

// C++ program to illustrate the use 
// of cin.ignore() 
#include <iostream> 

// used to get stream size 
#include <ios> 

// used to get numeric limits 
#include <limits> 
using namespace std; 

// Driver Code 
int main() 

     int x; 
     char str[80]; 
     cout << "Enter a number andstring:\n"
     cin >> x; 

     // clear buffer before taking 
     // new line 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

     // Input a string 
     cin.getline(str, 80); 
     cout << "You have entered:\n"
     cout << x << endl; 
     cout << str << endl; 

     return 0; 
}

 

INPUT:

8

Welcome to Gutilog

 

출력:

Enter a number and a string:

You have entered: 

8

Welcome to Gutilog

 

위 프로그램에서 cin.ignore()를 사용하지 않은 경우, 숫자를 입력한 후 사용자가 문자열을 입력하기 위해 Enter 키를 누르면 입력한 숫자만 출력된다. 프로그램은 문자열 입력을 받지 않는다. 이 문제를 피하기 위해 cin.ignore()를 사용하면 개행 문자가 무시되는 것이다.

 

728x90

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

C++ cerr 총정리  (0) 2024.03.10
C++에서 cout 총정리  (0) 2024.03.09
C++ 연산자(Operator) 총정리  (0) 2024.03.09
C++ if else문 총정리  (0) 2024.03.08
C++ if문 총정리  (0) 2024.03.08