简体   繁体   English

为什么int计数在进入循环后从1跳到4? C ++

[英]Why does int count jump from 1 to 4 on entering a loop? C++

My "counter" is jumping from 1 to 4 when I enter my loop. 进入循环时,我的“计数器”从1跳到4。 Any ideas? 有任何想法吗? Code and output below: 代码和输出如下:

    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;
    }
}

-1--4-Found log format: -1--4-找到的日志格式:
-4-cs-uri-stem: 0. cs(Referer): 0. COUNT: 4 -4-cs-uri-stem:0。cs(推荐人):0。COUNT:4
-5-cs-uri-stem: 0. cs(Referer): 0. COUNT: 5 -5-cs-uri-stem:0。cs(推荐人):0。COUNT:5
-6-cs-uri-stem: 0. cs(Referer): 0. COUNT: 6 -6-cs-uri-stem:0。cs(推荐人):0。COUNT:6
-7-cs-uri-stem: 0. cs(Referer): 0. COUNT: 7 -7-cs-uri-stem:0. cs(Referer):0. COUNT:7
-8-cs-uri-stem: 0. cs(Referer): 0. COUNT: 8 -8-cs-uri-stem:0. cs(Referer):0. COUNT:8
-9-cs-uri-stem: 0. cs(Referer): 0. COUNT: 9 -9-cs-uri-stem:0. cs(Referer):0. COUNT:9
-10-cs-uri-stem: 10. cs(Referer): 0. COUNT: 10 -10-cs-uri-stem:10。cs(推荐人):0。计数:10
-11-cs-uri-stem: 10. cs(Referer): 0. COUNT: 11 -11-cs-uri-stem:10。cs(推荐人):0。COUNT:11
-12-cs-uri-stem: 10. cs(Referer): 0. COUNT: 12 -12-cs-uri-stem:10. cs(Referer):0. COUNT:12
-13-cs-uri-stem: 10. cs(Referer): 0. COUNT: 13 -13-cs-uri-stem:10. cs(Referer):0. COUNT:13
-14-cs-uri-stem: 10. cs(Referer): 0. COUNT: 14 -14-cs-uri-stem:10. cs(Referer):0. COUNT:14
-15-cs-uri-stem: 10. cs(Referer): 0. COUNT: 15 -15-cs-uri-stem:10. cs(Referer):0. COUNT:15
-16-cs-uri-stem: 10. cs(Referer): 16. COUNT: 16 -16-cs-uri-stem:10. cs(Referer):16. COUNT:16
-17-cs-uri-stem: 10. cs(Referer): 16. COUNT: 17 -17-cs-uri-stem:10. cs(Referer):16. COUNT:17
-18-cs-uri-stem: 10. cs(Referer): 16. COUNT: 18 -18-cs-uri-stem:10. cs(推荐人):16. COUNT:18
-19-cs-uri-stem: 10. cs(Referer): 16. COUNT: 19 -19-cs-uri-stem:10. cs(Referer):16. COUNT:19
-20-cs-uri-stem: 10. cs(Referer): 16. COUNT: 20 -20-cs-uri-stem:10. cs(Referer):16. COUNT:20
Found field cs-uri-stem at position 10. 在位置10找到字段cs-uri-stem。
Found field cs(Referer) at position 16. 在位置16找到字段cs(Referer)。

I would bet that it's going throught the 我敢打赌,它会贯穿整个

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

and you never reset it after this branch 而且您在此分支之后再也不会重置它

Can't you attach a debugger and step through the code? 您不能附加调试器并逐步执行代码吗? It doesn't look like you are going to have many iterations to go through to see the answer. 看起来您将需要进行许多迭代才能看到答案。 (If this Visual Studio you could set a data break point on count at just hit run. I suspect GDB and most other debuggers will also support this.) (如果使用此Visual Studio,则可以在命中运行时在计数时设置数据断点。我怀疑GDB和大多数其他调试器也将支持此功能。)

You haven't provided all code in your loop - there's an unmatched open curly brace after the while statement. 您尚未在循环中提供所有代码-while语句后有一个无与伦比的打开花括号。 Do you have some cout code below what you've extracted? 您提取的内容下方是否有一些cout代码?

Perhaps your if statement right after the while loop begins comes back false? 也许恰好在while循环开始后的if语句返回false? The else attached to that if contains a count++ statement in a loop. 附加到if的else在循环中包含count ++语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM