Riscrivo quello fatto dall'altra parte. Questa funzione IMPEDISCE all'utente di digitare cose che tu non vuoi.
Si possono aggiungere anche controlli di formato, volendo. Comunque quello che vuoi fare tu si può fare anche in un altro modo: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(); }
codice:bool IsNumeric(char *string) { for(int i = 0; string[i] != '\0'; i++) { if ((string[i] < '0' || string[i] > '9') && string[i] != '.') return false; } return true; }

Rispondi quotando