cin 버퍼에 남은 글자수 세기 #include <iostream> #include <stdio.h> using namespace std; int main() { char ch; cout<<"Enter a string:"; cin>>ch; while(stdin->_cnt>1) { cout<<ch<<" "; cin>>ch; } cout<<ch; return 0; } c·c++/c++ 프로그래밍 2011.12.03
cin을 이용하여 정수를 안전하게 입력받기. #include <iostream> #include <limits> using namespace std; int main() { int num1; cout<<"\nEnter first number:\n"; cin>>num1; while(!cin.good()) { //this means while input is not valid, do this. // if input is valid, this code will not execute cin.clear(); //clear the error state //ignore all characters left in the buffer cin.ignore(numeric_li.. c·c++/c++ 프로그래밍 2011.12.03
c++ cin>> 대신 사용하는 안전한 입력방식 #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string input = ""; // How to get a string/sentence with spaces cout << "Please enter a valid sentence (with spaces):\n>"; getline(cin, input); cout << "You entered: " << input << endl << endl; // How to get a number. int myNumb.. c·c++/c++ 프로그래밍 2011.12.03