#include #include int read_in_base(int); void skip_rest_of_input_line (void); int main(void) { int i, n, base; printf("THIS PROGRAM CONVERTS NUMBERS IN A" "GIVEN BASE TO A DECIMAL NUMBER.\n\n"); do { printf("Enter number base (0 terminates the program, max 36): "); scanf("%d", &base); skip_rest_of_input_line(); if (base > 0){ printf("Enter a non-negative number to be converted" "to decimal notation: "); n = read_in_base(base); if (n >= 0) printf("The decimal number is: %d\n", n); else { printf("You have typed an illegal ciffer - TRY AGAIN\n"); skip_rest_of_input_line(); } } } while (base > 0); return 0; } /* Read a number in base and return the corresponding decimal number. Return -1 if erroneous input is encountered. */ int read_in_base(int base){ int ciffer_number, res = 0, done = 0; char ciffer; do { ciffer = getchar(); if (ciffer >= '0' && ciffer <= '9') ciffer_number = ciffer - '0'; else if (ciffer >= 'a' && ciffer <= 'z') ciffer_number = ciffer - 'a' + 10; else if (ciffer == '\n') done = 1; else return -1; if (ciffer_number >= 0 && ciffer_number < base){ if (!done) res = res * base + ciffer_number; } else return -1; } while (!done); return res; } void skip_rest_of_input_line (void){ char c; c = getchar(); while (c != '\n') c = getchar(); }