Io ho avuto un problema di questo tipo e risolsi con conio.h e getch. Esempio:
codice:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
void GetString(int lenght, char *buffer, bool allowedchars[256])
{
int i;
for(i = 0; i < lenght;)
{
int a = getch();
if (allowedchars[a])
{
buffer[i] = a;
putch(a);
i++;
}
}
buffer[i] = '\0';
}
int main()
{
bool OkChars[256];
for (int i = 0; i < 256; i++) OkChars[i] = false;
for (int i = 65; i < 91; i++) OkChars[i] = OkChars[i+32] = true;
char str[4];
GetString(3, str, OkChars);
printf("\n%s", str);
getch();
}
Questa versione permette anche di stabilire i caratteri permessi.