Sono un principiante in C, quindi per favore perdonami se questa domanda è stupida o se la domanda stranamente.Come mai una stampa viene eseguita dopo che una condizione vera è stata testata?
sto leggendo Primer C più e uno degli esempi in Capitolo-8 è qualche ciclo che testare se l'utente ha inserito - a newline character or not
, che non riuscivo a capire.
Il codice è corto, e vi mostrerò a voi:
int main(void)
{
int ch; /* character to be printed */
int rows, cols; /* number of rows and columns */
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf("%d %d",&rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width) // the function to preform the printing of the arguments being passed
Quello che non capisco è proprio qui:
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
Prima di tutto, il while (getchar() != '\n')
sta testando il primo ch era entrato giusto? In secondo luogo, se questo è vero, come mai il continuo non salta le dichiarazioni printf e va al primo momento? non è quello che dovrebbe fare?
Tnx
"Sto leggendo C primer plus" <--- Questo è il tuo più grande problema !! Uccidi quel libro con il fuoco !!! RTFN –
Il 'continue' è solo fumo. Sarebbe meglio essere 'while (getchar()! = '\ N');'. –
Grazie assegnato :) @Daniel Fischer – MNY