C++ 如何读取和计算输入文件中的单词,使用C+计算和打印每个单词的字母数量+;?

C++ 如何读取和计算输入文件中的单词,使用C+计算和打印每个单词的字母数量+;?,c++,eclipse,string,macos,file,C++,Eclipse,String,Macos,File,我是一名编程新手,有一项任务要求我创建一个程序,该程序将读取一个包含单词列表的文本文件,计算每个单词的单词总量和字母数量,并打印出一个包含单词数量和字母数量的输出文件,按类别从1个字母单词到13个字母单词 当我创建函数并试图让它读取文本文件中的单词时,它不允许我使用infle>>word读取它们的长度 我得到一个错误: “二进制表达式的操作数无效” 其他同学很顺利地使用了这个命令。我正在OSX El Capitan上使用EclipseMars.1 我遇到的另一个错误是在我的开关功能上,它对第一种

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

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

我得到一个错误:

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

其他同学很顺利地使用了这个命令。我正在OSX El Capitan上使用EclipseMars.1

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

“‘case’语句不在开关语句上”

提前谢谢

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;
}
void Words\u统计(std::ifstream&fin、std::ofstream&fout、std::string infle、std::string outFile)
{
//变量声明
infle=“words.txt”;
outFile=“Words\u Satistics.txt”;
字符串字;
整数totalWords=0;
整数字母宽度;
六、九月、休伊特、纽夫、迪克斯、昂兹、杜兹、特雷兹、奥特;
un=deux=trois=quatre=cinq=six=sept=huit=neuf=dix=onze=douze=treize=otre=0;
//打开要读入的输入文件
财务公开(填充);
if(fin.fail())
{

cout这里
infle>>word
infle
word
是一个
std::string
,因此对它们应用
操作符>
是没有意义的(毕竟不能右移字符串,这会产生意外的结果:)

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


开关
语句需要括号:

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


另一方面,
sizeof(word)
并不是你想象的那样。它得到的是
word
的实际大小,而不是
word
的字符数。你可以使用
word.length()
来实现:)

开关(lettsquantity)
->
开关(lettsquantity){/*…*/}
更不用说你的问题标题与你的错误完全无关。非常感谢你的观察!我忘记了括号。