简体   繁体   English

如何摆脱循环?

[英]How to get out of loop?

For a segment of my program, I am receiving a char array and checking if all the characters are ints. 对于程序的一部分,我收到一个char数组,并检查所有字符是否均为整数。

When the other program doesn't send a string of ints, like asdf , it catches that. 当其他程序没有发送ind字符串时,如asdf ,它将捕获该字符串。 But then it cycles through the loop four times before exiting. 但是然后它在退出之前循环通过循环四次。

How can I continue out of this segment immediately after it reads that the first char is not an int? 在读取到第一个字符不是整数后,如何立即退出该段? I've tried many things, but have been stuck with the same problem for a while. 我已经尝试了很多事情,但是一段时间以来都遇到了同样的问题。 I feel like it's a simple fix. 我觉得这很简单。

EDIT: I'd like for it to only print the Incoming connection received and INVALID section one. 编辑:我希望它只打印收到的传入连接和无效部分。 Then continue from there. 然后从那里继续。

    int iRecvB = recv(acceptSocket, recvSerial, STRLEN, 0);
    recvSerial[iRecvB] = '\0';
    if (iRecvB > 0)
    {
        for (int n = 0; n < iRecvB; n++)
        {
            if (recvSerial[n] < '0' || recvSerial[n] > '9')
            {
                isNumber = false;
            }
            if (!isNumber)
            {
                int iSend = send(acceptSocket, "no", 2, 0);
                cout << "\nIncoming connection received.\n" << "Data received:" << endl;
                cout << "\tSerial: " << recvSerial << endl;
                cout << "\t   INVALID\n" << endl;
                if (iSend == SOCKET_ERROR)
                {
                    cerr << "ERROR: Failed to send message11111\n";
                    cleanup(acceptSocket);
                    return 1;
                }
            }
        }// END OF FOR LOOP FOR NUM CHECK 
    }
    else if (iRecvB == 0)
    {
        cout << "Connection closed\n";
        cleanup(acceptSocket);
        return 0;
    }
    else
    {
        cerr << "ERROR: Failed to receive message\n";
        cleanup(acceptSocket);
        return 1;
    }

This is what the console displays: 这是控制台显示的内容:

Incoming connection received.
Data received:
        Serial: asdf
           INVALID


Incoming connection received.
Data received:
        Serial: asdf
           INVALID


Incoming connection received.
Data received:
        Serial: asdf
           INVALID

ERROR: Failed to send message11111

C:\Users\Admin\Desktop\Activation\ActivationServer\Debug>

You can use the break keyword. 您可以使用break关键字。

For instance: 例如:

for (int n = 0; n < iRecvB; n++)
{
    if (recvSerial[n] < '0' || recvSerial[n] > '9')
    {
        isNumber = false;
    }
    if (!isNumber)
    {
        int iSend = send(acceptSocket, "no", 2, 0);
        cout << "\nIncoming connection received.\n" << "Data received:" << endl;
        cout << "\tSerial: " << recvSerial << endl;
        cout << "\t   INVALID\n" << endl;
        if (iSend == SOCKET_ERROR)
        {
            cerr << "ERROR: Failed to send message11111\n";
            cleanup(acceptSocket);
            return 1;
        }

        break; // <-- HERE
    }
}// END OF FOR LOOP FOR NUM CHECK 

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

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