我寫了這段代碼,並且它運行良好,但是有一些格式錯誤,所以我改變了一些代碼,並添加了一個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)。 –