2016-06-26 3 views
-2

私はプログラミングのルーキーです。割り当てがあると、単語のリストを含むテキストファイルを読み込み、合計単語数と各単語の文字数を数え、出力ファイルを出力するプログラムを作成する必要があります1文字の単語から13文字の単語までのカテゴリ別の文字数xの単語の量入力ファイルの単語を読み込んでカウントする方法、C++を使用して1単語あたりの文字数をカウントして出力する方法は?

私の機能を作成し、テキストファイル内の単語を読み込もうとすると、inFile >> word;を使用してその長さを読み取ることはできません。

私はエラーを取得する:

"Invalid Operands To Binary Expressions".

他のクラスメートはトラブルもなく、このコマンドを使用しています。 OS X El CapitanでEclipse Mars.1を使用しています。

私が得ている別のエラーは、最初のケースで評価されるスイッチ機能にありますが、次のものでは評価されません。この場合、次のエラーメッセージが表示されます。

" 'case' Statement not on Switch statement".

ありがとうございます!

ここ
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; 
} 
+0

スイッチ(lettersQuantity) ' - スイッチ(lettersQuantity){/ * ... * /}' –

+0

あなたの質問のタイトルは、あなたが得るエラーとはまったく無関係です。 –

答えて

0

inFile >> word、​​とwordstd::stringであるので、それは彼らにoperator>>を適用しても意味がありません(あなたはすべての後に右シフト文字列をすることはできません、それは:)予期しない結果を与えるだろう)。

finあなたは switch文がカッコ必要:)


開かれたファイルであるあなたは、おそらく意味

fin >> word、:無関係なノートで

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

、「sizeof(word) doesnのをあなたが思うことはしないでください。実際のサイズはwordです。これは文字数ではありません。wordです。あなたはそれにword.length()を使用します:)

+0

あなたのご意見ありがとうございました!私は括弧を忘れていました。 – antonydelacruz

関連する問題