首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用C中的键盘中断FOR循环

用C中的键盘中断FOR循环
EN

Stack Overflow用户
提问于 2014-09-20 07:27:26
回答 4查看 2.5K关注 0票数 1

我有一个程序,它在启动for (;;)循环之前会运行几个步骤。

我希望能够中断;这个for循环使用击键(例如,“e”表示退出)。

我尝试过scanf_s,但我认为我没有正确地使用它们。如果我尝试在其中写入scanf_s,循环就不会启动。

我不太熟悉C编程,所以我有点挣扎。

有人能解释一下这件事吗?

额外信息:

操作系统是Win 8.1。

程序是TCP服务器程序。它创建一个套接字并开始监听。然后,它开始接受for循环中的客户端连接。然后开始另一个for循环以接收来自客户端的消息。

s**. 编辑:使用 this ,我成功地停止了使用的第二个for循环将更改添加到简化代码(只需在下面).**

添加:

代码语言:javascript
复制
if (_kbhit()) {
    key = _getch();
    if (key == 's');
    break;
}

以下是一些简化的代码:

代码语言:javascript
复制
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // prelim info goes here
    // like SOCKET 
    // and struct sockaddr_in

    // Initialize Winsock
    WSAStartup();

    // Create socket
    mysocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // server parameters go here 
    // like sin_addr and sin_port

    bind(mysocket);

    // start listening
    listen(mysocket, SOMAXCONN);

    // begin the damn for loops

    for (;;)
        {

        myclient = accept();

         //second for loop
        for (;;)
        {
            receivedata = recv(myclient);

            printf(<data>)

            if (_kbhit()) {
                key = _getch();
                if (key == 's');
                    break;
            }

        }
    }

    // close socket
    closesocket(mysocket);

   // call WSACleanup
   WSACleanup();

   return 0;
}

感谢所有提供链接和有帮助的答案的人。外部循环的问题被证明比仅仅破坏它更复杂一些。但是,如果有任何未来的访问者想知道如何用键盘按下一个循环,这已经实现了。看到正确的答案或我的简化代码。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-09-20 08:10:42

您可以尝试使用kbhitgetchar检查击键并相应地处理操作。查看这篇文章:Return pressed key without enter for confirmation

当按下e时,这段代码会中断while循环:多一个with循环更新的代码

代码语言:javascript
复制
#include <stdio.h>
#include <conio.h>

int main(){
    int ch = '1';
    while(ch != 'e'){                        //outer while for accepting connection
        printf("waiting for connection\n");  //do something

        while(ch != 'e'){                    //inner while for checking key press
            if(kbhit())                      //if key is pressed
                ch = getch();                //get the char and set ch to 'e'
        }
    }

    return 0;
}

使用中断的旧代码:

代码语言:javascript
复制
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main(){
    int ch;
    while(1){
        printf("*");
        if(kbhit()){               //if key is pressed
            ch = getch();          //get the char
            if(ch == 'e')          //if pressed key is 'e'
                break;             //break while loop 
        }
    }

    return 0;
}
票数 2
EN

Stack Overflow用户

发布于 2014-09-20 08:34:27

只需在for循环中输入这些行即可。当用户输入'e‘char时,这将退出。

代码语言:javascript
复制
    if(kbhit()){              
  ch = getch();          
  if(ch == 'e')          
    break;             
    }`
票数 0
EN

Stack Overflow用户

发布于 2014-09-20 08:52:15

假设微软的编译器,最简单的方法是使用它的conio库。我建议将细节封装到一个函数中,以允许重用。例如,您可能需要在进入第二个循环之前测试键盘。

代码语言:javascript
复制
#include <conio.h>

int nonBlockingGetch( void )
{
    int ch = -1 ;
    if( _kbhit() )
    {
        ch = _getch() ;
    }

    return ch ;
}

int main( void )
{
    int ch = -1 ;

    //  Outer loop
    while( ch != 'e' )
    {

        // inner loop
        while( ch != 'e' )
        {
            ch = nonBlockingGetch() ;
        }
    }

    return 0;
}

正如我所建议的,在某些情况下,如果请求退出,您可能不希望内部循环运行,在这种情况下:

代码语言:javascript
复制
//  Outer loop
while( ch != 'e' )
{
    ch = nonBlockingGetch() ;

    // inner loop
    while( ch != 'e' )
    {
        ch = nonBlockingGetch() ;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25946367

复制
相关文章

相似问题

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