n') 就是一直接收字符直到接收到的是回车.
另外,C语言的输入输出牵涉到一个缓冲机制,这里一直输入直到有一个回车才会从缓冲区读出数据.你不妨试一下编一个程序,输入N行,以EOF结尾,然后输出,你会发现,每输入一行按回车后,下面就会先输出你刚输入的那一行字符,然后再让你继续输入,直到遇上EOF(ctrl+z)相信这样会让你更好的理解.
根据ASCII码判断就差不多了吧。希望下面的代码对你有所帮助:
#include
#include
using namespace std;
int main()
{
char ch;
string str;
int charCount, intCount, spaceCount, otherCount;
charCount = intCount = spaceCount = otherCount = 0;
cout << "Please input a string:\n";
while(( ch = getchar() ) != EOF ) {
if(( ch <= 'z' && ch >= 'a' )||
( ch <= 'Z' && ch >= 'A' ))
charCount++;
else { if( ch == ' ' )
spaceCount++;
else { if( ch <= '9' && ch >= '0' )
intCount++;
else
otherCount++;
}
}
}
cout << "The number of character is " << charCount
<< "\nThe number of space is " << spaceCount
<< "\nThe number of integar is " << intCount
<< "\nOther number is " << otherCount - 1 << endl;
return 0;
}