用cin.get()输入字符,不知怎么终止
编了个简单的程序如下:#include <iostream>
using namespace std;
int main()
{
char ch;
// 初始化各个计数器
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int spaceCnt = 0, tabCnt = 0, newlineCnt = 0;
while (cin.get(ch)){
// 若ch是元音字母以及空格,制表符和换行符,将相应计数器加1
switch (ch) {
case 'a':
case 'A':
++aCnt;
break;
case 'i':
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
case ' ':
++spaceCnt;
break;
case '\t':
++tabCnt;
break;
case '\n':
++newlineCnt;
break;
}
}
// 输出结果
cout << "Numbers of vowel a: \t" << aCnt << "\n"
<< "Numbers of vowel e: \t" << eCnt << "\n"
<< "Numbers of vowel i: \t" << iCnt << "\n"
<< "Numbers of vowel o: \t" << oCnt << "\n"
<< "Numbers of vowel u: \t" << uCnt << "\n"
<< "Numbers of space character: \t" << spaceCnt << "\n"
<< "Numbers of Tab character: \t" << tabCnt << "\n"
<< "Numbers of newline character: \t" << newlineCnt << endl;
return 0;
}
运行结果时,不知道怎么终止,用"end"也不行,搞得很是郁闷。
望高手指点。
附送美女: