errors/input-2.c - Læsning af double og input med input validering - i do-while input validerings løkke. | Lektion 7 - slide 8 : 25 Program 2 |
#include <stdio.h> #include <stdlib.h> void clear_standard_input_line(void); void get_double_int_input(void); int main(void) { get_double_int_input(); return 0; } void get_double_int_input(void) { double x = 0.0; int i = 0, input_result; do { // Prompting for input of a double and an int: printf("Enter a double and an integer:\n"); input_result = scanf("%lf %d", &x, &i); if (input_result != 2){ clear_standard_input_line(); printf("Problems. Try again\n"); } } while (input_result != 2); // Proceed - input OK because input_result == 2; printf("x = %f, i = %d\n", x, i); } void clear_standard_input_line(void){ int ch; while ((ch = getchar()) != '\n' && ch != EOF); }