The scanf newline problem
Në shqip: Problemi i Enter-it pas scanf
ShpjegimiSQ
Pas scanf("%d", &n), Enter-i (\n) mbetet në hyrje — dhe fgets() ose getchar() i radhës e lexon menjëherë si rresht bosh. Zgjidhje: scanf(" %c") me hapësirë, ose lexo gjithçka me fgets dhe pastaj konverto.
EnglishEN
After scanf("%d", &n), the Enter (\n) stays in the input — and the next fgets() or getchar() reads it straight away as an empty line. Fix: scanf(" %c") with a space, or read everything with fgets and then convert.
Si ta mendosh
Si kur dikush lë një gotë bosh në tavolinë dhe kamerieri tjetër mendon se është porosia jote.
Lexoje në anglisht
Like someone leaving an empty glass on the table and the next waiter thinking it's your order.
Shembull kodic
int mosha; char emri[30];
scanf("%d", &mosha);
getchar(); // hiq Enter-in
fgets(emri, sizeof emri, stdin);