1

I am writing a simple Objective - C console app. I only want to accept digits. Even if i type a character which is not a digit, it should not be echoed back. and I should have a character array that has all digits on which i can use atoi().

scanf has formatters but they dont work the way i want. I know it should be possible. The way it works when u enter passwords in terminal, they are just accepted but not echoed back.

I just want a variation of it.

Is there some C function that only accepts a character and returns it but doesn't echoes it back on screen ?

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • By "scanf has formatters but they dont work the way i want" I guess you mean that the terminal echoes what you write? – Some programmer dude Dec 12 '11 at 10:49
  • If I were to do it in C on my windows machine, I would do something like this: `i = getch();` `if(isnum(i)) putchar(i)` – bill Dec 18 '15 at 22:31

4 Answers4

1

The characters are echoed by the terminal program, not your program. To control the terminal you have to use the termios functionality.

While the manual page linked to is for Linux, it is standardized for all UNIX variants.

Edit: A quick Google-search found this page: http://www.steve.org.uk/Reference/Unix/faq_4.html#SEC48

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    termios is in section 4 of the OS X man pages: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man4/termios.4.html. The equivalent to the page you linked is this one: http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man3/tcgetattr.3.html – JeremyP Dec 12 '11 at 11:44
1

Check out these link for more info What is Equivalent to getch() & getche() in Linux?

you can use getch() but if there is no getch() then you can try the below code.

#include <termios.h>
#include <stdio.h>

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  new = old; /* make new settings same as old settings */
  new.c_lflag &= ~ICANON; /* disable buffered i/o */
  new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
  tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void) 
{
  tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo) 
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

/* Read 1 character without echo */
char getch(void) 
{
  return getch_(0);
}

/* Read 1 character with echo */
char getche(void) 
{
  return getch_(1);
}

/* Let's test it out */
int main(void) {
  char c;
  printf("(getche example) please type a letter: ");
  c = getche();
  printf("\nYou typed: %c\n", c);
  printf("(getch example) please type a letter...");
  c = getch();
  printf("\nYou typed: %c\n", c);
  return 0;
} 

Getch function gets the characters but does not echo to screen.

Thanks

Community
  • 1
  • 1
niko
  • 9,285
  • 27
  • 84
  • 131
  • 1
    I don't know about Objective-C (one of the tags in the question), but having a variable named `new` might be a _bad idea™_ if ever it will be ported to C++. – Some programmer dude Dec 12 '11 at 11:58
0

Look into using some terminal-I/O library (such as the venerable ncurses), since you need to do terminal-specific things to make the terminal's input "raw", so that you can input one character at a time and decide from your program if the character is to be echoed or not.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Let me introduce you to the beautiful and yet elegantly simple scanset.

Dark Star1
  • 6,986
  • 16
  • 73
  • 121