/* This program illustrates the definition and use of a very simple
   function to convert Fahrenheit temperatures into Celsius

   It has been modified with additional functions to get input,
   convert to Kelvin temperatures, and print the state of water
   at that temperature as well.
   */

#include <iostream.h>

float Fahr2Cel(float);  /* Function to convert Fahrenheit to Celsius */
float Fahr2Kel(float);  /* Function to convert Fahrenheit to Kelvin */
char GetChoice();       /* Function to get menu choice from user */
void PrintState(float); /* Function to print state of water */

/* The main function will prompt the user for the Fahrenheit temperature,
   and print either the corresponding Celsius or Kelvin temperature */

main() {
   float f,     /* Fahrenheit temperature entered by user */
         conv;  /* Converted temperature printed to screen */
   char option; /* Whether the user wants to convert to Celsius or Kelvin */

   /* Prompt the user for a Fahrenheit temperature */
   cout << "Enter a Fahrenheit temperature: ";
   cin >> f;

   /* Function call to GetChoice, which returns either 'c' or 'k' */
   option = GetChoice();

   /* switch on option, executing either the Fahr2Cel or the 
      Fahr2Kel function */
   switch(option) {

     case 'c': /* convert to Celsius */
       conv = Fahr2Cel(f);
       break;

     case 'k': /* convert to Kelvin */
       conv = Fahr2Kel(f);
       break;

     }

   cout << "The corresponding Celsius temperature is "
        << conv << "\n";

   /* Call function to print state of water at that temperature. Since it
      does not return anything, note that it is not part of an assignment
      statement. */

   PrintState(f);

   } /* end of main function */


/* The Fahr2Cel function takes a float value as a parameter, and returns the
   corresponding Celsius temperature. */

float Fahr2Cel(float fahr_temp) {

   float cel_temp; /* corrsponding Celsius temperature */

   /* Compute the corresponding Celsius temperature and return it */
   cel_temp = 5 * (fahr_temp - 32)/9;
   return cel_temp;

   } /* end of Fahr2Cel function

/* The Fahr2Kel function takes a float value as a parameter, and returns the
   corresponding Kelvin temperature. */

float Fahr2Kel(float ftemp) {

   float ctemp, /* corrsponding Celsius temperature */
         ktemp; /* corrsponding Kelvin temperature */

   /* First use the Fahr2Cel function to get the corresponding Celsius temp */

   ctemp = Fahr2Cel(ftemp);

   /* Then add 273 to get the Kelvin temp and return it */
   ktemp = ctemp + 273;
   return ktemp;

   } /* end of Fahr2Kel function

/* The GetChoice function takes no parameters. Instead, it prompts the user
   to choose to either convert to Celsius or Kelvin (continuing to prompt if
   neither is chose), and returns the corresponding character (either 'c' or
   'k') */

char GetChoice() {

   char answer;  /* letter entered by user */

   cout << "Convert to c)elsius or k)elvin: ";
   cin >> answer;

   /* Keep prompting while answer not legal */
   while (answer != 'c' && answer != 'k') {
     cout << "Please enter c or k: ";
     cin >> answer;
     }

   /* Return the user's choice */
   return answer;
  
   } /* end of GetChoice function */

/* The PrintState function takes a float representing a Fahrenheit temperature
   as its parameter, and prints either "solid", "liquid", or "gas", depending
   on the state of water at that temperature. It returns nothing (void). */

void PrintState(float temperature) {
  
   /* Nested if statement which compares the temeprature to 32 and 212 */

   if (temperature < 32) {
     cout << "Water is a solid at that temperature.\n";
     }
   else {
     if (temperature < 212) {
       cout << "Water is a liquid at that temperature.\n";
       }
     else {
       cout << "Water is a gas at that temperature.\n";
       }
     }

   } /* end of PrintState method */

