/* Write a program which asks the user to input a single c or f. If they input a 'c' this means that they want to convert degrees C to degrees F. If they input a 'f' they want to convert degrees F to degrees C. In each case print out a table going from -40 to 40 degrees. Make sure the user can repeat the process if they want. suggested solution:*/ #include <stdio.h> #define TRUE 1 #define FALSE 0 /* Function Prototypes or Allusions */ int test(char); void ctof(void); void ftoc(void); int main() { char inchar; printf("Enter c or f (or anything else to stop)\n"); scanf("%c",&inchar); fflush(stdin); /* An aha moment on the next line */ while(test(inchar)) { /* note you don't have to test for f...why? */ if(inchar == 'c' || inchar == 'C') ftoc(); else ctof(); printf("Enter c or f (or anything else to stop)\n"); scanf("%c",&inchar); fflush(stdin); } } /* This is a user define function (a function definition) which validates their input it takes one character argument and returns TRUE if that character is F,f,c or C otherwise it returns false */ int test(char tester) { switch(tester) { case 'f': return TRUE; case 'F': return TRUE; case 'c': return TRUE; case 'C': return TRUE; default: return FALSE; } } void ctof(void) { float c; printf("C degrees\tF degrees\n"); printf("---------\t---------\n"); for(c= -40;c <= 40; c=c + 5.0) printf("%f\t%f\n",c,c * 1.8 +32.0); } void ftoc(void) { float f; printf("F degrees\tC degrees\n"); printf("---------\t---------\n"); for(f= -40;f<=40; f=f + 5.0) printf("%f\t%f\n",f,(f - 32.0)/1.8); }