读书人

请问一个关于CIN的有关问题

发布时间: 2012-05-14 15:24:34 作者: rapoo

请教一个关于CIN的问题

C/C++ code
#include <iostream>const int ArSize=10;void strcount(const char *str);int main(){    using namespace std;    char input[ArSize];    char next;    cout<<"Enter a line: \n";    cin.get(input,ArSize);    while(cin)    {        cin.get(next);        while(next!='\n')            cin.get(next);        strcount(input);        cout<<"Enter next line(empty line to quit): \n";        cin.get(input,ArSize);    }    cout<<"Bye\n";    return 0;}void strcount(const char *str){    using namespace std;    static int total=0;    int count =0;    cout<<"\""<<str<<"\"contains";    while(*str++)        count++;    total+=count;    cout<<count<<"characters\n";    cout<<total<<"charaters total\n";}

此段代码中
while(cin)以及 cin.get(next)
请问这2处的cin是如何收到值的,程序执行的时候只是在cin.get(input,ArSize);输入了值

[解决办法]
while(cin)是判断当前的流状态是否处于可用。
cin.get(next),在当前输入缓冲区中读入一个字符到next.
C/C++ code
 cin.get(next);        while(next!='\n')            cin.get(next); 

读书人网 >C++

热点推荐