简体   繁体   中英

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. (IE quit xyz) and lets the user try again rather than exiting the server. Unfortunately the server quits whether the user types quit or quit arguments.

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 .

Your loop structure seems missing brace before while to be wrong, or it is a typo. Check it first and lets see the correct code.

Remove your test on the cmd in the while condition. Whenever he user chooses 'quit' it evaluates to false, so the whole expression is false due to the conjunction and the loop exits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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