GeneNight
GeneNight

Reputation: 35

Taking as input only numbers and letters with getch() in c

what I'm trying to accomplish is a simultaneous research by using the fuction getch. I know that this fuction returns a integer value, which represents the ASCII code of a key. Is there any wroten fuction that allows the user to write only certain characters, such as: letters, numbers, and apostrophe.

Upvotes: 1

Views: 696

Answers (2)

chux
chux

Reputation: 154075

Is there any write function that allows the user to write only certain characters, such as: letters, numbers, and apostrophe. (?)

Code can instead selectivity write

#include <ctype.h>
#include <stdio.h>

int c; // use type `int` here to distinguish all (256) characters from EOF
while((c = getch()) != '\r' && c != '\n'  && c != EOF) {
  if (isalnum(c) || '\'') {
    putchar(c);
  }
}

Upvotes: 0

GeneNight
GeneNight

Reputation: 35

char c;
int temp = 0;
while((c= getch()) !='\r')
{
    if ((temp == -32) || (temp == 0))
    {
    }
    else
    {
        if(isalnum((char)c) == 0)
        {
            if((c == '\'') || (c == -118) || (c == -115) || (c == -107) || (c == -123) || (c == -105))
            {
                printf("true: %c\n",c);
            }
            else
                printf("false: %d\n",c);
        }
        else
        {
            printf("true: %c\n",c);
        }
    }
    temp = c;
}

For anyone else who's trying to accomplish the same result, this works fine for me.

Upvotes: 1

Related Questions