如何读取和计数输入文件中的单词,计数和打印每个单词的字母数量

How to read and count the words in an input file, count and print the amount of letters per word using C++?

本文关键字:单词 打印 文件 何读取 读取 输入      更新时间:2023-10-16

我是一个编程新手,有一项任务要求我创建一个程序,该程序将读取一个包含单词列表的文本文件,计算单词总数和每个单词的字母数量,并打印出一个输出文件,该文件按类别从1个字母单词到13个字母单词的单词数量为x个字母。

当我创建我的函数并尝试让它读取文本文件中的单词时,它不允许我使用inFile >> word;读取它们的长度。

我得到错误:

"二进制表达式的无效操作数".

其他同学使用这个命令没有麻烦。我在OS X El Capitan上使用Eclipse Mars.1。

我得到的另一个错误是在我的开关功能上,它对第一种情况进行评估,但对以下情况不进行评估。在这种情况下,我得到以下错误消息:

" 'case'语句不在Switch语句上".

提前感谢!

void Words_Statistics(std::ifstream & fin, std::ofstream & fout, std::string inFile, std::string outFile)
    {
    // Variable Declaration
    inFile="words.txt";
    outFile="Words_Satistics.txt";
    string word;
    int totalWords=0;
    int lettersQuantity;
    int un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, onze, douze, treize, otre;
    un = deux = trois = quatre = cinq = six = sept = huit = neuf = dix = onze = douze = treize = otre=0;
    // Open input file to read-in
    fin.open(inFile);
        if(fin.fail())
        {
            cout << inFile << " Failed to open file."<< endl;
            exit (1);
        }
        do {
            (inFile >> word);
            lettersQuantity = int (sizeof(word));
            totalWords++;
            lettersQuantity+=lettersQuantity;
                switch (lettersQuantity)
                    case 1:
                        un++;
                        break;
                    case 2:
                        deux++;
                        break;
                    case 3:
                        trois++;
                        break;
                    case 4:
                        quatre++;
                        break;
                    case 5:
                        cinq++;
                        break;
                    case 6:
                        six++;
                        break;
                    case 7:
                        sept++;
                        break;
                    case 8:
                        huit++;
                        break;
                    case 9:
                        neuf++;
                        break;
                    case 10:
                        dix++;
                        break;
                    case 11:
                        onze++;
                        break;
                    case 12:
                        douze++;
                        break;
                    case 13:
                        treize++;
                        break;
                    default:
                        otre++;
                        break;
        }
        while (!fin.eof());
    int avg = lettersQuantity / totalWords;
}

这里inFile >> word, inFilewordstd::string,因此对它们应用operator>>是没有意义的(毕竟你不能右移字符串,这会产生意想不到的结果:))。

您可能指的是fin >> word,其中fin是您打开的文件:)


switch语句需要括号:

switch (lettersQuantity)
{ //Note bracket
case 1: //....
//....
}

在一个无关的注意,sizeof(word)不做你认为它做什么。它获得word的实际大小,而不是word的字符数量。您可以使用word.length():)