一个简单任务:逐字符地读取来自文件或键盘的文本
void textin1()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit:\n";
cin >> ch;
while (ch != '#')
{
cout << ch;
++count;
cin >> ch;
}
cout << endl << count << " characters read\n";
}
cin.get(char)读取输入中的下一个字符(即使它是空格),并将其赋给变量ch。
void textin2()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit:\n";
cin.get(ch);
while (ch != '#')
{
cout << ch;
++count;
cin.get(ch);
}
cout << endl << count << " characters read\n";
}
如果输入来自文件,则可以使用一种功能更强大的技术——检测文件尾(EOF)。
原文:https://www.cnblogs.com/adtxl/p/11477870.html