2017-02-16 13 views
0

このコードを書いて、うまくいきましたが、何らかのフォーマットエラーがあったので、コードを変更しました。コード内のコメント。ループで何もしない何かを変更した後にC++が無限ループに陥る

#include <string> 
#include <iostream> 
#include <fstream> 
#include <ctype.h> 

using namespace std; 

int main(int argc, char* argv[]) { 

/*if (argc != 3) { 
    cout << "Error: wrong number of arguments." << endl; 
}*/ 

ifstream infile(argv[1]); 
//infile.open(argv[1]); 

string content((std::istreambuf_iterator<char>(infile)), 
    (std::istreambuf_iterator<char>())); 

string final; 
string temp; 
string distinct[5000]; 
string look; 
int distinctlen = 0; 
int distinctnum[5000] = { 0 }; 
int numdist = 0; 
int wordcount = 0; 
int i = 0; 
int j = 0; 
int k = 0; 
int quest = 0; 
int isdistinct = 0; 
int ismatch = 0; 
int qmatch = 0; 
int len = content.length(); 
bool wordprinted = false; 
//cout << "test 1" << endl; 
//cout << "length of string: " << len << endl; 
//cout << "content entered: " << content << endl; 
while (i < len) { 

    //cout << "test 2" << endl; 
    if (isalpha(content[i])) { 
     //cout << "test 3" << endl; 
     if (isupper(content[i])) { 
      //cout << "test 4" << endl; 
      temp.push_back(tolower(content[i])); 

     } 
     else { 
      //cout << "test 5" << endl; 
      temp.push_back(content[i]); 

     } 
    } 
    else { 
     //cout << temp << endl; 
     //cout << "test 6" << endl; 
     ++wordcount; 
     final = final + temp; 
     j = 0; 
     for (k = 0;k < numdist;k++) { 
      //cout << "test 7" << endl; 
      if (distinct[k] == temp) { 
       ++distinctnum[k]; 
       isdistinct = 1; 
       break; 
      } 
     } 
     if (isdistinct == 0) { 
      //cout << "test 8" << endl; 
      distinct[numdist] = temp; 
      ++numdist; 
     } 
     temp.clear(); 
     isdistinct = 0; 
    } 
    //cout << temp << endl; 
    ++i; 
} 
//cout << final << endl << endl; 
cout << "The number of words found in the file was " << wordcount + 1 << endl; 
cout << "The number of distinct words found in the file was " << numdist + 1 << endl << endl; 

ifstream infile2(argv[2]); 
string query((std::istreambuf_iterator<char>(infile2)), 
    (std::istreambuf_iterator<char>())); 
//cout << query << endl; 
query += ' '; 

int len2 = query.length(); 
int looklen; 
//cout << quest << endl; 
for (i = 0;i < len2;i++) { 
    if (query[i] == '?') { 
     quest = 1; 
    } 
    else if (isspace(query[i])) { 
     //cout << "test1" << endl; 
     if (quest == 0) { 
      //cout << "test2" << endl; 
      for (j = 0;j < numdist;j++) { 
       if (look == distinct[j]) { 
        ismatch = 1; 
        cout << look << " : matches " << look << " " << distinctnum[j]+1 << " time(s)." << endl; 
        break; 
       } 
      } 
      if (ismatch == 0) { 
       cout << look << " : no match." << endl; 
      } 
      ismatch = 0; 
     } 
     else { 
      //cout << "test" << endl; 
      looklen = look.length(); 
      //cout << looklen << " " << look << endl; 
      for (j = 0;j < numdist;j++) { 

       for (k = 0;k < looklen;k++) { 

        //cout << k << endl; 
        if (looklen < distinct[j].length()) { 
         break; 
        } 
        if (look[k] == '?' && (k + 1) == looklen && wordprinted == false) { 
         cout << look << " : matches " << distinct[j] << " " << distinctnum[j] + 1 << " time(s)." << endl; 
         k++; 
         wordprinted = true; //NEW LINE ADDED 
         cout << "wordprinted = true" << endl; 
         break; 
        } 
        else if (look[k] == '?' && (k+1) == looklen && wordprinted == true) { //NEW CODE ADDED THAT BROKE IT 
         for (i=0;i < looklen;i++) { 
          cout << " "; 
         } 
         cout << " "; 
         cout << "matches " << distinct[j] << " " << distinctnum[j] + 1 << " time(s)." << endl; 
         break; 
        } 
        else if (look[k] == distinct[j][k]) { 
         k++; 
        } 
        else if (look[k] == '?') { //check for space 
         k++; 
         continue; 
        } 

        else if (look[k] != distinct[j][k]) { 
         break; 
        } 
        if ((k + 1) == looklen) { 
         //cout << "test3" << endl; 
         cout << look << " : matches " << distinct[j] << " " << distinctnum[j] + 1 << " time(s)." << endl; 
         break; 
        } 

       } 

      } 
      wordprinted = false; 
      cout << "wordprinted = false" << endl; 
     } 
     look.clear(); 
     continue; 
    } 
    look.push_back(query[i]); 
    //cout << "test" << endl; 
    cout << len2 << endl; 

} 
return 0; 

}私は取得しています出力から

わずかな抜粋:

wordprinted = falseを
wordprinted = falseを
wordprinted = false
a ?? :マッチと4回。
wordprinted = true
は1回と一致します。
は1回と一致します。
はwordprinted =偽
wordprinted =偽
wordprinted =偽
? :マッチと4回。
wordprinted = true
は1回と一致します。
は1回と一致します。

+0

新しい 'if'を見つけるのに困っている人は、' else if(look [k] == '?' &&(k + 1)== looklen && wordprinted == true) ' –

+1

あなたは質問をすることになっています。 – nwp

+2

あなたの例は長すぎます。実際に何をしているのか、この機能が何を期待しているのかは不明です。あなたはあなたの事例を単純化し、期待された観察された行動を明確にしなければなりません。参考例の作成の詳細については、[このリンク](http://stackoverflow.com/help/mcve)を参照してください。 –

答えて

2

新しいコードの中でiインデックスを再利用しました。これは毎回外部ループからカウンタをリセットします。ネストされたforループには注意が必要です。

1つの方法では、カウンタ変数の宣言をプログラムの先頭で停止することを避けることができます。

for(int i=0;i<len2;i++) 

その後、コンパイラはカウンタ変数を再利用したことを警告していました。変数の範囲を試して最小化し、上記のような設定された状況でインスタンス化してください。複数の目的のためのグローバル変数と再利用変数は特に悪いです。 「私」のような匿名のカウンターは大丈夫ですが、意味をなさない特定のスコープでそれらを宣言します。

関連する問題