fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. float c2f(float);
  6. float f2c(float);
  7.  
  8. float Fahrenheit,Celsius;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.  
  13. /**
  14.  * Check for the expected number of arguments (3)
  15.  * (0) program name
  16.  * (1) flag
  17.  * (2) temperature
  18.  */
  19. if (argc!=3)
  20. {
  21. printf("Incorrect number of arguments\n");
  22. exit(0);
  23. }
  24.  
  25. if (!strcmp(argv[1], "toF"))
  26. {
  27. // convert the string into a floating number
  28. char *check;
  29. float Celsius = strtod(argv[2], &check);
  30.  
  31. if (Celsius < -273.15)
  32. {
  33. printf("ERROR! The temperature is below absolute zero.");
  34. }
  35.  
  36. // process from celsius to fahrenheit
  37. Fahrenheit = c2f(Celsius);
  38. printf("%5.2f°C = %5.2f°F\n",Celsius, Fahrenheit);
  39. }
  40. else if (!strcmp(argv[1], "toC"))
  41. {
  42. // convert the string into a floating number
  43. char *check;
  44. float Fahrenheit = strtod(argv[2], &check);
  45.  
  46. // process from fahrenheit to celsius
  47. Celsius = f2c(Fahrenheit);
  48. printf("%5.2f°F = %5.2f°C\n", Fahrenheit, Celsius);
  49. }
  50.  
  51. else
  52. {
  53.  
  54. printf("Invalid flag\n");
  55. }
  56. } // main
  57.  
  58.  
  59. float c2f(float c)
  60. {
  61. return 32 + (c * (180.0 / 100.0));
  62. }
  63.  
  64. float f2c(float f)
  65. {
  66. return (100.0 / 180.0) * (f - 32);
  67. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Incorrect number of arguments