【发布时间】:2015-08-31 04:53:45
【问题描述】:
我的程序从正在使用的输入中读取字母和数字。但我不知道如何将其实现为 .txt 文件。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
int countLetters = 0, countDigits = 0;
cout << "Enter a line of text: ";
cin.get(ch);
while(ch != '\n'){
if(isalpha(ch))
countLetters++;
else if(isdigit(ch))
countDigits++;
ch = toupper(ch);
cout << ch;
//get next character
cin.get(ch);
}
cout << endl;
cout << "Letters = " << countLetters << " Digits = " << countDigits << endl;
return 0;
}
我在硬件中犯了一个错误,我应该计算 .txt 文件中的单词而不是字母。我无法计算单词,因为我对单词之间的空格感到困惑。我怎么能改变这个代码来计算单词而不是字母?非常感谢您的帮助。
【问题讨论】:
-
在网络上搜索“C++ 读取文本文件”。尝试自己做,遇到具体问题再回来。