我写了这段代码,并且它运行良好,但是有一些格式错误,所以我改变了一些代码,并添加了一个else语句,我标记了一个在代码中发表评论。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 =假
wordprinted =假
wordprinted = false
一个?? :比赛和4次(S)。
wordprinted = true
相当于1次(s)。
符合1次(s)。
wordprinted =假
wordprinted =假
wordprinted =假
的ΔΣ :比赛和4次(S)。
wordprinted = true
相当于1次(s)。
符合1次(s)。
对于可能无法找到新的if的所有人,请搜索此行:else if(look [k] =='?'&&(k + 1)== looklen && wordprinted == true) ' –
你应该问一个问题。 – nwp
你的例子太长了,目前还不清楚你希望这个函数做什么,而不是它实际上在做什么。你将不得不简化你的例子,并澄清预期和观察到的行为。有关生成有用示例的更多信息,请参阅[此链接](http://stackoverflow.com/help/mcve)。 –