2017-02-20 15 views
1

私の問題は、単語カウントをリセットする方法が正確ではないということです。私は単語検索を作成しましたが、10個の異なる単語の出現数を数えれば、それは最初の単語から同じ数のままです。私はforループ複数の単語の単語の出現数をカウントする問題

出力

boy appeared 3 times 
Snape appeared 3 times 
Dumbledore appeared 3 times 
he appeared 3 times 
her appeared 3 times 
the appeared 3 times 
it appeared 3 times 
is appeared 3 times 
will appeared 3 times 
all appeared 3 times 

それは私のコードを読み取ることで

boy appeared 3 times 
Snape appeared 7 times 
Dumbledore appeared 4 times 
he appeared 27 times 
her appeared 4 times 
the appeared 13 times 
it appeared 6 times 
is appeared 12 times 
will appeared 2 times 
all appeared 3 times 

のようになります。私は私がきたと確信しているを使用します。ここで、私がいる問題であると考えていますそれはそれをより複雑にしました。私が作ったアドバイスや修正は感謝しています。

全コード事前に

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <vector> 

// Main Function 
int main() 
{ 
    // Declaration 
    std::string list, passage, word[10]; 
    std::ifstream listFile("WordList.txt", std::ios::in); 
    std::ifstream passageFile("HarryPotterPassage.txt", std::ios::in); 
    std::vector<std::string> vec_wordList, vec_passage; 


    /* Read a file that contains a list of 10 words */ 
    if (listFile.is_open()) 
    { 
     // Store text file in a vector 
     while (listFile) 
     { 
      listFile >> list; 
      vec_wordList.push_back(list); 
     } 

     // Assign vector to individual strings 
     for (int i = 0; i < 10; i++) 
     { 
      word[i] = vec_wordList[i]; 
     } 

     // Close file 
     listFile.close(); 
    } 
    else 
     std::cout << "No file found.\n"; 


    /* Read another file containing a paragraph */ 
    if (passageFile.is_open()) 
    { 
     while (passageFile) 
     { 
      // Store text file in a string 
      std::getline(passageFile, passage); 
     } 

     // Close file 
     passageFile.close(); 
    } 
    else 
     std::cout << "No file found.\n"; 

    //std::cout << passage << '\n'; 


    /* Count the number of words from the first file 
     from the second file that contains the paragraph */ 
    size_t count = 0; 
    std::string::size_type pos = 0; 

    for (int i = 0; i < 10; i++) 
    { 
     while ((pos = passage.find(word[i], pos)) != std::string::npos) 
     { 
      count++; 
      pos += word[i].size(); 
     } 

     std::cout << word[i] << " appeared " << count << " many times\n"; 
    } 

    system("pause"); 
    return 0; 
} 

感謝。

+0

おそらく、 'std :: unoredered_map 'を使って解決するほうが簡単でしょう。 –

+0

私は 'for = 'ループの各繰り返しの始めに' count = 0'と 'pos = 0'を設定する必要があると思います。基本的には、これらの2つの宣言を**そのループに移動することができます。 –

答えて

0

あなたは、外側のループの各反復の初めにcountposをリセットする必要があります。言い換えれば

は、この変更:これに

size_t count = 0; 
std::string::size_type pos = 0; 
for (int i = 0; i < 10; i++) 
{ 
    ... 
} 

for (int i = 0; i < 10; i++) 
{ 
    size_t count = 0; 
    std::string::size_type pos = 0; 
    ... 
} 

をところで、私はまたsizeof(word)/sizeof(*word)にその10を変更すると思います。

+0

C++ 11以降では 'for(const auto&w:word)'を使用し、ループ内の 'word [i] 'を' w'で置き換えることさえできます。 –

+0

うわー!私はそれがとても単純なものであることを知っていました。なぜそれがリセットされないのかを理解しようとすると、私に24時間頭痛を与えていた。どうもありがとう。 – gomicoo

+0

@gomicoo:問題ありません。ところで、私は自分の解決策をテストすることさえ気にしませんでした。私はあなたが単語 '[i]'ごとに異なる 'count'を必要としていることは明らかでした。そこから単語' i 'ごとに異なる 'pos'が必要だと私は結論づけました。上記のコメントに示唆されている追加の修正に注意してください。 –

1

単語[i]の代わりに単語[9]を使用すると、それぞれの単語ではなく最後の単語の結果が得られます。 試し:

for (int i = 0; i < 10; i++) 
{ 
    while ((pos = passage.find(word[i], pos)) != std::string::npos) 
    { 
     count++; 
     pos += word[i].size(); 
    } 

    std::cout << word[i] << " appeared " << count << " many times\n"; 
} 
+0

ダーン、それを忘れてしまった。それで私はもともと単語[i]を使っていたのです。私は数字を変更していたので、実際にどのような結果が出るのか知っています。それを単語[i]に変更することも私にとってはうまくいかない。 – gomicoo

関連する問題