首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么int计数在进入循环时从1跳到4?C++

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

Stack Overflow用户
提问于 2009-09-18 15:22:16
回答 4查看 671关注 0票数 0

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

代码语言:javascript
复制
    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-找到日志格式:

-4-cs-uri-干: 0。政务司司长(推荐):0。计数:4

-5-cs-uri-干: 0。政务司司长(推荐):0。计数:5

-6-cs-uri-阀杆: 0。政务司司长(推荐):0。计数:6

-7-cs-uri-干: 0。政务司司长(推荐):0。计数:7

-8-cs-uri-干: 0。政务司司长(推荐):0。计数:8

-9-cs-uri-干: 0.政务司司长(推荐):0。计数:9

- 10 -cs-uri-茎:10。cs(推荐者):0。计数: 10

-11-cs-uri-茎: 10。cs(推荐者):0。计数: 11

-12-cs-uri-茎: 10。cs(推荐者):0。计数: 12

-13-cs-uri-茎: 10。cs(推荐者):0。计数: 13

-14-cs-uri-茎: 10。cs(推荐者):0。计数: 14

-15-cs-uri-茎: 10。cs(推荐者):0。计数: 15

- 16 -cs-uri-阀杆: 10。cs(推荐者):16。计数:16。

- 17 -cs-uri-阀杆: 10。cs(推荐):16。计数:17。

- 18 -cs-uri-阀杆: 10。cs(推荐):16。计数:18。

- 19 -cs-uri-阀杆: 10。cs(推荐):16。计数:19。

- 20 -cs-uri-阀杆: 10。cs(推荐):16。计数:20。

在10号位置找到了领域cs-uri-阀杆。

在位置16处找到cs字段(推荐者)。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-09-18 15:33:44

我敢打赌它一定会通过

代码语言:javascript
复制
                        while (!foundField.eof())
                        {
                                foundField >> field;
                                //if (count == cs_uri_stemLocation) cout << field << endl;
                                count++;
                        }

你从来没有在这个分支之后重新设置它

票数 6
EN

Stack Overflow用户

发布于 2009-09-18 15:39:48

您不能附加调试器并逐步完成代码吗?看起来你不会有太多的迭代来找到答案。(如果此,则只需单击run即可在count上设置数据断点。我怀疑GDB和大多数其他调试器也会支持这一点。)

票数 1
EN

Stack Overflow用户

发布于 2009-09-18 15:31:12

您还没有在循环中提供所有代码-在while语句之后有一个无与伦比的打开大括号。你在提取的下面有一些cout代码吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1445237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档