/********************* * Name: Geoff Towell * Purpose: Convert Celcius to Fahrenheit * Doing it manually to preven scanf loop * Wrttten: August 5, 2019 * Modified: Sep 6, 2019 * Sep 12 and 17, 2019 *********************/ #include #include #include "c2fsafe2.h" #define FREEZING 32.0 #define SCALE 1.8 #define CC(v) (FREEZING + SCALE*v) /** * Read one complete line of text and extract an integer from that line * ie everything to the \n from the keyboard. * so '123' is translated into the integer 123. * Stop accumulating into the integer on the first non-digit. * That is '123abd123' would yeild 123 * Also the integer must start the line. So 'a123' whould return 0. * Known limitations: this function does not handle negative numbers * does not handle integers larger than max-int **/ int main(void) { double f; int c; while (1) { printf("Enter a Celcius temperature: "); c = myatoi(); if (c<-273.15) { printf("The number you entered %d is below absolute zero\nSo it has not been converted to Fahrenheit\n", c); } else { f = CC(c); printf("Celcius: %d Fahrenheit: %.1f\n", c, f); } } return 0; } int myatoi() { int keepAdding=1; int result = 0; char inChar; while ('\n' != (inChar=getchar())) { if (keepAdding && '0'<=inChar && '9'>=inChar) { result = result*10 + (inChar-'0'); } else { keepAdding=0; } } return result; }