我正在尝试计算输入文件中的大写和小写字母、位数和单词数。我已经完成了这个,但是,我的字数减少了一个。输入文件中有 52 个单词,但我的计数是 53。这是什么原因造成的?所有其他(大写,小写和数字)都是正确的......
这是我所拥有的:
using namespace std;
int main()
{
fstream inFile;
fstream outFile;
string fileName("");
string destName("");
char c = 0;
///////string wrdRev("");/////////
int numCount = 0;
int capCount = 0;
int lowCount = 0;
int wordCount = 0;
cout << "Please enter file name: ";
getline(cin, fileName);
cout << endl;
inFile.open(fileName, ios::in);
if (inFile.good() != true) {
cout << "File does not exist!\n" << endl;
return 0;
}
else{
reverse(fileName.begin(), fileName.end());
destName += fileName;
}
outFile.open(destName, ios::in);
if (outFile.good() == true){
cout << "File '" << destName << "' already exists!\n" << endl;
return 0;
}
else {
outFile.clear();
outFile.open(destName, ios::out);
while(inFile.good() != false){
inFile.get(c);
if(isupper(c)){
capCount++;
}
else if(islower(c)){
lowCount++;
}
else if(isdigit(c)){
numCount++;
}
else if(isspace(c)){
wordCount++;
}
}
outFile << "There are " << capCount << " uppercase letters." << endl;
outFile << "There are " << lowCount << " lowercse letters." << endl;
outFile << "There are " << numCount << " numbers." << endl;
outFile << "There are " << wordCount << " words." << endl;
}
inFile.close();
outFile.close();
return 0;
}
任何帮助,将不胜感激。谢谢你。