How input the number of digits without using Enter I can do it with Enter:
scanf("%4d",&num);
So how I can do that without press Enter?
How input the number of digits without using Enter I can do it with Enter:
scanf("%4d",&num);
So how I can do that without press Enter?
To avoid the need to hit enter you need to disable icanon
mode using the ioctl
system call (or stty
command - assuming you are using Linux). You probably want to use getchar instead of scanf
and do your own input processing. It is very difficult.
You can maybe look at this post:
as i said in comment above and as a reply to @Rabbid76
getch() and putch() are non-standard functions defined in conio.h, mostly used in turbo C/dev C++ environement. getchar() are putchar() are standard functions defined in C standard and they can be used in all environments. Yes, you can certainly write a program without these 4 functions from google
but if some one insists here is how it can be (not handling negative numbers and backspace)
int inputDgits(int nb){
int i=0;
int j=0;
char tmp;
for( j=0; j < nb; ){
tmp=getch();
if (tmp>='0' && tmp<='9'){
i *= 10;
i += tmp-'0';
j++;
printf("%c",tmp);
}else{
// beep;
}
}
return i;
}
Thank you,but I mean to his:
int i = 0;
for (i=0;i<4;i++)
{
getche();
}