01: // come risolvere problema buffer di tastiera
02: #include<stdio.h>
03: #include<stdlib.h>
04:
05: int main(int argc, char **argv){
06:
07: char sc;
08: char str[100];
09:
10:
11: printf("Inserisci un carattere: ");
12: sc=getchar();
13: printf("Ho letto il carattere '%c'\n", sc);
14:
15: // se l'utente ha inserito un singolo carattere nel buffer di tastiera finisce: il carattere + invio (\n)
16: // la getchar() rimuove il carattere, ma lo '\n' rimane. Letture successive potrebbero venir confuse dalla
17: // presenza dello '\n'
18: // posso rimuovere usando un secondo getchar()
19:
20: //getchar(); // remove the \n
21:
22: // Ma c'e' da fidarsi sul fatto che l'utente abbia premuto un solo tasto? -> NO
23: while(getchar()!='\n'); // remove the whole keyboard buffer up to the first '\n'
24:
25:
26:
27:
28: printf("Inserisci una parola: ");
29: scanf("%[^\n]", str);
30: printf("Ho letto la stringa '%s'\n", str);
31:
32:
33: return 0;
34: }
35:
36:
Se avete commenti o osservaƶioni su questa pagina
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it