计算文本文件中的字母

Lil*_*ems 0 c++

可以告诉我这个代码有什么不对吗?它编译和一切都很好,但输出是固定的零一直下降.所以它不算数字.

#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;
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*fin 7

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

   while (!inMyStream.eof() )
   {
       //get a line of text
       getline (inMyStream, lineBuffer);
Run Code Online (Sandbox Code Playgroud)

你应该立刻学到的一件事是,当你写完它时,这将无法正常工作.你通常想做的是:

while (getline(inMyStream, lineBuffer)) {
    // .. the rest of the processing.
Run Code Online (Sandbox Code Playgroud)

但是,由于您一次只处理一个字符,而忽略除字母之外的所有字符,因此一次只能读取一个字符可能更简单:

int ch;
while (inMyStream >> ch)
// process the character
Run Code Online (Sandbox Code Playgroud)

由于没有其他人提及它们,我还要指出,不是明确地测试'a'和'z'来找到小写字母,而'A'和'Z'来找到大写字母,你会更好使用islowerisupper,在<ctype.h>(在其他几个地方)提供:

#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'];
Run Code Online (Sandbox Code Playgroud)


Pas*_*uoq 6

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

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]++;
Run Code Online (Sandbox Code Playgroud)

(至少)这两个中的一个65是错误的.我会推荐int('A')int('a')不是......

注意:这可能不是解释您的问题的原因.

  • +1,但它只是'a'或'A',周围没有`int()`. (2认同)