C++ 计算文本文件中的字母数

C++ 计算文本文件中的字母数,c++,C++,sommebody能告诉我这个代码有什么不对吗?它可以编译,一切都很好,但是输出是稳定的,一直到零。因此,它不算字母 #include <iostream> #include <fstream> #include <string> using namespace std; const char FileName[] = "c:/test.txt"; int main () { string lineBuffer; ifstream in

sommebody能告诉我这个代码有什么不对吗?它可以编译,一切都很好,但是输出是稳定的,一直到零。因此,它不算字母

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:/test.txt";

int main () 
{
    string lineBuffer;
    ifstream inMyStream (FileName); //open my file stream


    if (inMyStream.is_open()) 
    {
       //create an array to hold the letter counts
       int upperCaseCount[26] = {0};
           int lowerCaseCount[26] = {0};

       //read the text file
       while (!inMyStream.eof() )
       {
           //get a line of text
           getline (inMyStream, lineBuffer);
           //read through each letter in the lineBuffer
           char oneLetter;
           for( int n=0; n < (int)lineBuffer.length(); ++n )
           {
             oneLetter = char( lineBuffer[n] ); //get a letter
                if (oneLetter >= 'A' && oneLetter <='Z') 
                { //decide if it is a capital letter
                     upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                         if (oneLetter >= 'a' && oneLetter <='z') 
                         { //decide if it is a lower letter
                               lowerCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                         }//end 
                }//end
           }
        }//end of while

        inMyStream.close(); //close the file stream

        //display the counts
        for (int i= 0; i < 26; i++)
            cout << char(i + 65) << "\t\t" << lowerCaseCount[i] << char(i + 95) << "\t\t" << lowerCaseCount[i] << endl;
}//end of if
        else cout << "File Error: Open Failed";

       return 0;
}

编辑:事实上,这里描述的问题不是唯一的问题,请参阅其他答案以获得更完整的解决方案

upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                     if (oneLetter >= 'a' && oneLetter <='z') 
                                     { //decide if it is a lower letter
                           lowerCaseCount[int(oneLetter)- 65]++;
这两个65中至少有一个是错误的。我会推荐int'A'和int'A'来代替

注意:这可能并不能解释您的问题。

您的if-about大写和小写字母嵌套不正确。如果一个字母不是大写字母,你甚至不看小写字母。这两个国际单项体育联合会应处于同一级别

这是我能看到的唯一错误

我建议按照gf的建议进行调试,或者加入一些打印语句,以验证您对正在发生或未发生的事情的假设。

您的if语句在这里的作用域是错误的。每个字母可以是大写,也可以是小写,但是如果你的if语句是限定范围的,你只需要检查字母是否已经是大写,这当然是没有意义的

你想要的东西更像:

for(unsigned n = 0; n < lineBuffer.length(); ++n)
{
   oneLetter = char( lineBuffer[n] ); // get a letter
   if (oneLetter >= 'A' && oneLetter <='Z') {
     upperCaseCount[int(oneLetter)- 'A']++;
   }
   else if (oneLetter >= 'a' && oneLetter <='z') { 
     lowerCaseCount[int(oneLetter)- 'a']++;
   }
}

这段代码可能还有其他错误,但最突出的一点是,计算小写字母的if语句位于计算大写字母的if语句中。您的测试文件可能不包含任何大写字母,因此输出为实零

应有两个独立的if语句,如:

if (oneLetter >= 'A' && oneLetter <='Z') 
{ //decide if it is a capital letter
  upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array   
}//end

if (oneLetter >= 'a' && oneLetter <='z') 
{ //decide if it is a lower letter
  lowerCaseCount[int(oneLetter)- 65]++; //make the index match the count array
}//end 

在你知道自己遇到的问题上,你已经得到了一些帮助,现在也许有一点你可能还没有意识到:

   while (!inMyStream.eof() )
   {
       //get a line of text
       getline (inMyStream, lineBuffer);
您应该马上了解的一件事是,正如您所写的那样,这将无法正常工作。您通常要做的是:

while (getline(inMyStream, lineBuffer)) {
    // .. the rest of the processing.
但是,由于一次只处理一个字符,并且忽略除字母以外的所有内容,因此一次只读取一个字符可能会更简单:

int ch;
while (inMyStream >> ch)
// process the character
由于没有其他人提到过它们,我还要指出,与其显式地测试“a”和“z”来查找小写字母,“a”和“z”来查找大写字母,不如使用islower和isupper,这是在其他几个地方提供的:

#include <ctype.h>

while (inMyStream >> ch)
    if (isupper((unsigned char)ch))
        ++upperCount[(unsigned char)ch-'A'];
    else if (islower((unsigned char)ch))
        ++lowerCount[(unsigned char)ch-'a'];

最后的打印结果如何,小写字母计数打印两次?这就解释了为什么一直都是零,因为原始代码正确地计算了大写字母,不是吗?

您是否尝试过在调试器中运行您的程序,并逐步观察实际发生的情况?不,我不知道怎么做。您使用的是什么操作系统和开发环境?我有Visual Studio 2008,正在运行Vista@LilProblems:试着进入调试阶段,从长远来看,它将真正帮助您。参见本教程第9页及其后:好球!小写字母从97开始,如果我记得正确的话,嗯,他很可能是要铸造,也就是int’a。我没有注意到C++标签,所以我在模仿附近的iToNeLtter的语法。我似乎没有得到C++的挂起,虽然现在我知道它是C++。而传统的C型演员是国际级的-@黑客:在C中,字符常量有类型int,但是在C++中它有类型char。如果你对char和int都重载f,f'a'将称为char重载。谢谢大家,你们都很好,就像一个符咒一样完美。等一下,我今晚还有两份作业要交。哈哈@法拉兹:第二个if语句的逻辑与注释不匹配,至少与我所知道的任何字符集都不匹配。无论字母是大写还是小写,都要减去65,但“a”和“a”显然没有相同的值……应该有一个if语句,使用toupper或tolower转换字符,然后进行比较。另外,去掉神奇的数字65,用“A”替换。编辑:我的错,原来的帖子包含两个区分大小写的if语句。我仍然支持用“A”或“A”替换65。在这项作业中,我已经收到了代码,我只需要修改它以计算小写和大写。我们不应该更改任何给定的代码。我知道-那太糟糕了吧?可惜你不能更改代码,因为如果有一个循环为所有字符创建计数,然后只打印出大写和小写字母,肯定会简单得多。尝试一下,看看有多少复杂性被删除了!