* cin.peek()

 

제일 앞 글자 하나만 문자로 가져온다.

 

 

예)

#include <iostream>
using namespace std;

 

int main()
{
 char test;
 cout<<"무엇이든 입력하세요: ";
 test = cin.peek();
 cout << endl << test;
}

 

156 입력 ==> 1 출력

apple 입력 ==> a 출력

 

 

응용)

#include <iostream>
#include <string>
using namespace std;

 

int main()
{
 char test;
 double number;
 string str;


 cout<<"무엇이든 입력하세요: ";
 test = cin.peek();


 if(test>='0' and test <= '9'){
  cin >> number;
  cout << "입력한 숫자는" << number << endl;
 }


 else{
  cin >> str;
  cout << "입력한 단어는" << str << endl;
 }

}

 

3.14 입력 ==> 3.14 출력

apple 입력 ==> apple 출력

 

 

 

* cin.ignore()

 

원하는 것이 나올 때까지 건너뛴다.

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

[C++] cout.width() 출력할 값의 길이 지정  (1) 2016.05.27
[C++] sqrt (제곱근) 사용하기  (0) 2016.05.27

printf 함수는 %5d, %2f 등으로 출력할 숫자의 자릿수를 지정할 수 있다.

반면 cout은 그 방법이 묘연했는데, 다음과 같은 함수를 사용할 수 있다는 것을 발견.

 

 

cout.width(number)

 

예)

#include <iostream>
using namespace std;

int main()
{
 cout.width(5);
 cout<<1.23578797;
}

 

출력: 1.23579

 

cout.width를 선언한 바로 다음 한 번의 출력에만 적용된다.

* 헤더파일: #include <cmath>

* 형식: std::sqrt(number)

 

* using namespace std; 선언하면, sqrt(number)로 사용 가능

 

* sqrt(152.2756L)과 sqrt(152.2756)의 차이:

 

 

+ Recent posts