繁体   English   中英

每次在C中输入后如何自动按“ ENTER”键?

[英]How to automatically press 'ENTER' key after every input in C?

我正在制作一个软件,在该软件中,每次用户输入数字后,都会自动按ENTER键(我的意思是,将在不按ENTER的情况下分配值)。

样例代码:

#include<stdio.h>

int main(){
int i, num[10];

for(i=0; i<10; i++){
    scanf("%d", &num[i]);  

    /*this is where I need help. 
    Every time after typing a single number, the program should store the value
    and move on to next position without requiring the user to press ENTER. How do I
    do that ? */
}

您可以使用getch()函数,但只能读取0到9之间的数字。接下来,可以使用(int)(ch)-48)转换为int,例如:

char ch;
ch=getch();
printf("%d", (ch - '0'));

/*You can do this too*/
// printf("%d", ((int)(ch)-48));  

数字48是ascii表中的“ 0”。

如果要避免如给定答案中建议的那样从conio.h getch() ,请使用stdio.h getchar()

暂无
暂无

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

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