2

I was making a software where, every time after the User would enter a number, automatically ENTER key will be pressed (I mean, The value will be assigned without pressing ENTER ).

Sample code:

#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 ? */
}
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Yeal Takian
  • 41
  • 1
  • 1
  • 6
  • 3
    You can't do this in pure C I'm afraid. Have a frustrating upvote. – Bathsheba Aug 12 '18 at 18:33
  • Input from the console is buffered. Meaning that your program won't get fed any input until enter is pressed. Google for "raw i/o" to learn more about how to do this for your particular os. – selbie Aug 12 '18 at 18:34
  • If you just want a single digit, use %c – phuclv Aug 12 '18 at 18:36
  • 1
    Possible duplicate of [breaking loop with keypress in linux c](https://stackoverflow.com/questions/15310848/breaking-loop-with-keypress-in-linux-c) – bmargulies Aug 12 '18 at 18:41
  • 3
    Is the 'single number' you refer to a single digit (`0` .. `9`), or can it include multi-digit numbers (`10` .. `∞`)? With single digits, it is easier because you can't tell you've reached the end of a number until you type something that isn't a digit (space, newline, etc). This is partly a question of terminology, of course. You may need to read about [Canonical vs non-canonical terminal input](https://stackoverflow.com/questions/358342/). – Jonathan Leffler Aug 12 '18 at 19:09
  • 1
    You cannot in pure C. Full stop. That being said different OS can offer different ways. It is possible both in Windows and in Unix or Linux, but not in a portable way. Comments above are for Unix way, conio is the DOS/Windows way. – Serge Ballesta Aug 12 '18 at 21:10
  • As there is no standard C way, you need to specify operating system and on Windows also compiler. – hyde Aug 13 '18 at 14:50

2 Answers2

3

You can use the getch() function, but only reads a number from 0 to 9. Next, you can convert to int by using (int)(ch)-48), example:

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

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

The number 48 is the '0' in the ascii table.

Fábio Morais
  • 272
  • 1
  • 9
  • 1
    That is conio.h function. Not really very recommended, because it's not standard snd iw kind if DOS/Windows terminal specific... – hyde Aug 12 '18 at 20:33
  • @hyde There is no non-platform-specific way to obtain this functionality. – Sneftel Aug 13 '18 at 05:39
  • @Sneffel Yes, but conio.h is still not very good option. For example, last MSVC to have it, seems to be 2015, looking at versions here: https://msdn.microsoft.com/en-us/library/7x2hy4cx.aspx. At the very least, answer should mention where getch() comes from. – hyde Aug 13 '18 at 08:36
  • @hyde You right, but I think it's the only way to do this in C. – Fábio Morais Aug 13 '18 at 13:44
  • 1
    @FábioMorais Of course it isn't the only way. You could use win32 API directly, for instance (that's what *conio* does internally). Anyway, user does not specify OS or compiler, so there's no way to know if this answer is valid for them or not. – hyde Aug 13 '18 at 14:48
  • 3
    don't use a magic number like 48. No one knows what it is. And it's not guaranteed to be 48 on other charset. Use `'0'` instead – phuclv Aug 14 '18 at 11:04
  • 1
    no. You're still using the magic number. Use `ch - '0'` instead – phuclv Aug 14 '18 at 11:44
  • Ok, I used both ways, I think is fine now. – Fábio Morais Aug 14 '18 at 11:48
1

If you want to avoid getch() from conio.h as suggested in the given answer, use getchar() from stdio.h.

P.W
  • 26,289
  • 6
  • 39
  • 76