#include /* * scanf is the function you use to read interactive input. * * While it is essentially simple to use if you know exaactly what kind * of input to expect, it is not easy at all to handle real-world cases * where the uses will very likely input the wrong data. * * E.g.: you ask a Yes/No question and ask the user to type 'Y' for Yes * and 'N' for No. The user writes "yes", or "y", or "si"... * */ int main(void) { int anInt; float aFloat; int result = 0; // scanf returns the number of conversions succesfully performed. // We consider our template to be applied when both numbers are // correctly read. while (result != 2) { printf("Inserisci un intero e un decimale: "); // %d matches an integer number // %f matches a float number (use %lf to match a double) // Note: scanf arguments are always _pointers_!! result = scanf("%d %f", &anInt, &aFloat); // result equals 2 if both numbers have been read if (result == 2) { printf("Hai inserito: %d e %f\n", anInt, aFloat); break; } // user input doesn't match the specification. // read and discard the user input ("*" modifier). // assume the input is a string for generality. // try removing this line, input something like // "2abc 5" and see what appens!!! scanf("%*s"); printf("Lettura fallita. Per favore, riprova.\n"); } return(0); }