简体   繁体   English

为什么我的“ Do While”循环会终止?

[英]Why does my Do While loop terminate?

I am trying to implement a server with multiple commands. 我正在尝试使用多个命令来实现服务器。 Most commands work so far except that I want the server to send a warning to the client when the client writes an argument to the quit command. 到目前为止,大多数命令都可以正常工作,只是我希望服务器在客户端向quit命令写入参数时向客户端发送警告。 (IE quit xyz) and lets the user try again rather than exiting the server. (即IE退出xyz),并让用户重试而不是退出服务器。 Unfortunately the server quits whether the user types quit or quit arguments. 不幸的是,无论用户类型是quit还是quit参数,服务器都会退出。

bool done = false;

do
{
    if(strcmp(cmd, "quit") == 0)
    {
        if(strcmp(argument, "") != 0)
            strcpy(replyMsg, "504 Command not implemented for that parameter.\n");
        else
        {
            strcpy(replyMsg,"221 Service closing control connection.\n");
            done = true;
        }
    }
    while(strcmp(cmd, "quit") != 0 && done != true);

I would just use: 我只会用:

...
}
while(!done);

There is no point checking cmd . 没有检查cmd点。

Your loop structure seems missing brace before while to be wrong, or it is a typo. 你的循环结构似乎缺少括号之前while是错误的,或者它是一个错字。 Check it first and lets see the correct code. 请先检查一下,然后查看正确的代码。

Remove your test on the cmd in the while condition. 在while条件下,在cmd上删除测试。 Whenever he user chooses 'quit' it evaluates to false, so the whole expression is false due to the conjunction and the loop exits 每当他的用户选择“退出”时,其计算结果均为false,因此由于合取,整个表达式均为false,并且循环退出

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

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