Problem:

istream& get(char& ch) 함수를 이용하여 한 라인을 읽고 빈칸(' ')이 몇 개인지 출력하는 프로그램을 작성하라.

Execution Result:

Objective & Hints:

cin으로 키 입력 연습

Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include using namespace std; int main() { char ch; int cnt; while(true){ cin.get(ch); // 키를 ch에 읽어옴 if(cin.eof()) // EOF 문자 즉 ctrl-z 키가 입력된 경우, 읽기 종료 break; if(ch == '\n') // 키가 입력된 경우 읽기 중단 break; else if(ch == ' ') cnt++; } cout << cnt; }

](