【发布时间】: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