C++ 为什么整数计数在进入循环时从1跳到4?C++;

C++ 为什么整数计数在进入循环时从1跳到4?C++;,c++,visual-studio,loops,counter,increment,C++,Visual Studio,Loops,Counter,Increment,当我进入循环时,我的“计数器”从1跳到4。有什么想法吗?代码和输出如下: static bool harvestLog() { ifstream myFile("LOGS/ex090716.log"); if (myFile.fail()) {cout << "Error opening file";return 1;} else { cout << "File opened... \n"; stri

当我进入循环时,我的“计数器”从1跳到4。有什么想法吗?代码和输出如下:

    static bool harvestLog()
{
    ifstream myFile("LOGS/ex090716.log");
    if (myFile.fail()) {cout << "Error opening file";return 1;}
    else
    {
        cout << "File opened... \n";
        string line;
        string field;
        int cs_uri_stemLocation = 0;
        int csReferrerLocation = 0;
        int count = 1;
        cout << "-" << count << "-";
        while( getline(myFile, line) ) {
            if ( strstr(line.c_str(), "cs-uri-stem") &&
                (strstr(line.c_str(), "cs(Referer)") || strstr(line.c_str(), "cs(Referrer)")) )
            {
                cout << "-" << count << "-";
                cout << "Found log format: \n";
                istringstream foundField(line);
                while (!foundField.eof())
                {
                    cout << "-" << count << "-";
                    foundField >> field;
                    if (field == "cs-uri-stem") {cs_uri_stemLocation = count;}
                    if (field == "cs(Referer)" || field == "cs(Referrer)") {csReferrerLocation = count;}
                    cout << "cs-uri-stem: " << cs_uri_stemLocation << ". ";
                    cout << "cs(Referer): " << csReferrerLocation << ". ";
                    cout << "COUNT: " << count << endl;
                    count++;
                }
                cout << "Found field cs-uri-stem at position " << cs_uri_stemLocation << "." << endl;
                cout << "Found field cs(Referer) at position " << csReferrerLocation << "." << endl;
                count = 1;
            }
            else
            {
                count = 1;
                istringstream foundField(line);
                while (!foundField.eof())
                {
                    foundField >> field;
                    //if (count == cs_uri_stemLocation) cout << field << endl;
                    count++;
                }

                //cmatch results;
                //regex rx("(?:p|q)(?:=)([^ %]*)");
                //regex_search(line.c_str(), results, rx);
                //string referringWords = results[1];

                //cout << referringWords;
            }
        }
    myFile.close();
    return 0;
    }
}
static bool-harvestLog()
{
ifstream myFile(“LOGS/ex090716.log”);

如果(myFile.fail()){cout您的循环中没有提供所有代码-while语句后面有一个不匹配的大括号。在您提取的代码下面有一些cout代码吗?

我敢打赌它正在通过

                        while (!foundField.eof())
                        {
                                foundField >> field;
                                //if (count == cs_uri_stemLocation) cout << field << endl;
                                count++;
                        }
while(!foundField.eof())
{
foundField>>字段;

//如果(count==cs\u uri\u stemLocation)不能您不能附加一个调试器并逐步完成代码吗?看起来您不需要经过很多次迭代才能看到答案。(如果这个Visual Studio可以在点击运行时设置count的数据断点。我怀疑GDB和大多数其他调试器也会支持这一点。)

也许while循环开始后的if语句返回false?附加到该if的else在循环中包含count++语句。

尝试了LogParser?想自己写一个。添加一点功能;-)看起来您缺少while循环上的结束括号。是否在if语句之后立即关闭它或者while循环还有其他功能吗?很抱歉,现在整个函数都有了。哪个循环?我看到不止一个带有计数器的循环。为了获得最佳答案,您应该修剪代码,直到无法再获得奇怪的行为,然后发布准确的代码。很少有人会对涉过一段甚至无法复制的代码感兴趣我很抱歉,我现在已经包括了整个函数。我试图缩小问题的范围,但由于缺乏外部性而造成的麻烦是不值得的。