fix problem when invalid data is passed via stdin

This commit is contained in:
Chris Cromer 2018-11-12 14:46:02 -03:00
parent a6731de4cd
commit d9aab389a1
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A

View File

@ -179,12 +179,12 @@ int main (int argc, char **argv) {
for (i = 0; i < n; i++) {
if (elegir) {
opt = 0;
while (opt <= 1) {
fprintf(stdout, "Elegir elemento %lli: ", i + 1);
fscanf(stdin, "%d", &opt);
if (opt <= 1) {
while (!fscanf(stdin, "%d", &opt)) {
// Falló, pide de nuevo después de limpiar stdin
while ((opt = getchar()) != '\n' && opt != EOF) { }
fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n");
}
fprintf(stdout, "Elegir elemento %lli: ", i + 1);
}
array[i] = opt;
}