改变与循环无关的东西后C ++陷入无限循环

时间:2017-02-16 14:57:47

标签: c++ loops

我编写了这段代码并且运行正常,但是有一些格式错误,所以我更改了一些代码并添加了一个else if语句,我在代码中用注释标记。

#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
  22个
  wordprinted = false
  22个
  22个
  22个
  wordprinted = false
  22个
  22个
  22个
  一个?? :比赛和4次   wordprinted = true
        匹配为1次         匹配1次   wordprinted = false
  22个
  wordprinted = false
  22个
  22个
  22个
  wordprinted = false
  22个
  22个
  22个
  一个?? :比赛和4次   wordprinted = true
        匹配为1次         匹配1次。

1 个答案:

答案 0 :(得分:2)

您在新代码中重用了i索引。每次都会从外循环重置计数器。嵌套for循环要小心。

您可以避免的一种方法是停止在程序顶部声明计数器变量。在for循环的标题中声明它们,总是:

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

然后编译器会提醒您重用了一个计数器变量。尝试并最小化变量的范围,并在如上所述的设置环境中实例化它们。全局和重用变量用于多种目的尤其糟糕。像'我'这样的匿名计数器是可以的,但是用它们有意义的特定范围声明它们。

相关问题