【问题标题】:How to read and count the words in an input file, count and print the amount of letters per word using C++?如何读取和计算输入文件中的单词,使用 C++ 计算和打印每个单词的字母数量?
【发布时间】:2016-10-28 17:42:17
【问题描述】:

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

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

我得到错误:

“二进制表达式的无效操作数”。

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

我遇到的另一个错误是我的 switch 功能,它评估第一种情况,但不适用于以下情况。在这种情况下,我收到以下错误消息:

"'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;
}

【问题讨论】:

  • switch (lettersQuantity) -> switch (lettersQuantity) { /* ... */ }
  • 更不用说你的问题标题与你得到的错误完全无关。

标签: c++ eclipse string macos file


【解决方案1】:

这里inFile &gt;&gt; wordinFilewordstd::string,因此将operator&gt;&gt; 应用于它们是没有意义的(毕竟你不能右移字符串,这会给意想不到的结果:))。

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


switch 语句需要括号:

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

在不相关的注释中,sizeof(word) 并没有做你认为它做的事情。它得到word 的实际大小,这不是word 的字符数。你会使用word.length() :)

【讨论】:

  • 非常感谢您的观察!我忘了括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2016-08-06
  • 2011-09-05
  • 2023-02-11
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多