C++ 如果cout丢失,则循环进入无限循环

C++ 如果cout丢失,则循环进入无限循环,c++,string,while-loop,infinite-loop,endl,C++,String,While Loop,Infinite Loop,Endl,我遇到了一些非常奇怪的事情。我遇到问题的代码是: int stringPos; int found1; while (stringPos < 1); { //start searching inString for framen starting at foundn and record found1 = inString.find(frame1, found1); cout << found1 << endl; //if ret

我遇到了一些非常奇怪的事情。我遇到问题的代码是:

int stringPos;
int found1;
while (stringPos < 1);
{
    //start searching inString for framen starting at foundn and record
    found1 = inString.find(frame1, found1);
    cout << found1 << endl;


    //if return is a number, push back foundn to a vector
    if (found1 != -1)
    {
        foundPositions.push_back(found1);
    }
    //if return is npos, then break the loop
    else
    {
        stringPos=1;
    }

    //add 1 to foundn so that the search would continue from where the
    //search ended last
    found1+=1;
}

奇怪的是,当我放置cout时,您的程序有未定义的行为,因为您试图在此处使用未初始化变量的值:

while (stringPos < 1)
//     ^^^^^^^^^
//     This is uninitialized

此外,即使假设您的变量已初始化,您也有一个分号,使您的while循环成为no op或无限循环。

这是一个错误,使用了一个单位化变量:

while (stringPos < 1);
因为它相当于:

while (stringPos < 1) {}
如果这没有进入无限循环,那么它后面的代码将只执行一次。要更正:

初始化变量stringPos和found1。 对stringPos使用类型size\u t,发现as不返回int,但返回size\u类型通常是size\u t。 使用而不是-1测试是否未找到。 移除尾随分号。
我将从初始化stringPos和found1变量开始。

该代码并不像您所说的那样。如果你不给我们看有问题的代码,我们如何解决它?请阅读并思考
while (stringPos < 1) {}