Midterm Temp Converter 50 points 2016 This assignment is a s

Midterm: Temp Converter (50 points)                                                                                               2016

This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An added option is to display the degrees Kelvin (absolute zero scale). Please note the ‘required elements’ discussed below in order to have a complete and correct program. This will be done as a command line program, as shown in the sample run.

Welcome to the Temperature Converter!

Would you also like to see degrees Kelvin (K) in the results? (Y/N): Y

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1

Enter your Fahrenheit temperature: 32

A temp of 32 Fahrenheit converted to Celsius = 0 C.

     This is also a temperature of: 273.15 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2

Enter your Celsius temperature: 100

A temp of 100 Celsius converted to Fahrenheit = 212 F.

   This is also a temperature of: 373.15 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2

Enter your Celsius temperature: -300

A temp of 100 Celsius converted to Fahrenheit = -508 F.

   This temperature is not possible, as it is below 0 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 3

Would you also like to see degrees Kelvin (K) in the results? (Y/N): N

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1

Enter your Fahrenheit temperature: -40

A temp of 32 Fahrenheit converted to Celsius = -40 C.

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 0

Thanks for using the temperature converter!

(the bold and italics above is for emphasis only, you do not have to print your results in a different typeface).

The Formulas for the conversions are as follows:

Co = 5/9(Fo - 32)

Fo = (9/5 Co) + 32

K = Co + 273.15

Thus, the program sample of converting 32 degrees F to C was: (5/9)*(32-32) = 0.

Likewise, the conversion of 100 degrees C to F was: (9 / 5 * 100) + 32 = 212.

Note that at the beginning of the program it asks if degrees K are to be included in the output, but this setting can be changed by selecting option 3 during a session.

Pseudo-code has been supplied for the exercise, and you should follow the program design/structure as shown in the Pseudo-code. Other items that must be present for full credit are:

1)Minimize the code in main, but in that method include the primary loop which allows the user to process multiple conversions.

2)Develop a separate getCType() method to perform data validation on the integer input value which specifies the conversion method (F, C, Start/Stop K, or quit). In this method use a try/catch structure to avoid the program crashing on illegal values (such as string input when a number is requested).

3)Develop separate methods for each of the conversion calculations. These conversion methods do not need to return values, but just calculate and display the proper result. They should be called FtoC(), CtoF() and showDegreesK()

a.Use data type double for the temperature values.

4)Develop a getTemp() method used by both FtoC() and CtoF() which operates like getCType() (discussed above in step 2); it should accept and error trap the temp input value and then return a legal value to the FtoC() or CtoF() calling method. (Validation in this step does not require recognizing a temperature below absolute zero).

5)To accomplish the Degrees K component you must store the original user decision (as to whether or not they want to see K), and, if needed, at the end of each conversion method, call a different method to produce the additional ‘degrees K’ output line. This is the method to be called showDegreesK(). Design note: you should not need two methods for the degrees K result – only a single showDegreesK() method is needed. (why?)

Also, inside the showDegreesK() method write a validation routine which recognizes an illegal temperature and prints an appropriate message. In other words, since the kelvin scale is based on absolute zero, a negative result for degrees K is actually illegal, and a message should be shown instead of the calculated value (see example above).

Call your program TempConverter and zip the full project folder and submit it in the usual way (there will be a ‘midterm’ link in the assignments area).

Solution

PROGRAM CODE:

/*
* TempCoverter.cpp
*
* Created on: 23-Nov-2016
* Author: kasturi
*/


#include <iostream>
#include <cmath>
using namespace std;

//global varaible that stores the choice for kelvin temp
char Kchoice;

//method get the temperature from user and check for validations
float getTemp()
{
   float temp;
   cin>>temp;
   if(isnan(temp))
       throw \"Invalid input\ \";
   else return temp;
}

//method to get the input choice from user and do validations
int getCType()
{
   int choice;
   cin>>choice;
   if(isnan(choice))
       throw \"Invalid input.\ \";
   else return choice;
}

//method that prints the kelvin temperature for celsius based on the menu choice
void showDegreesK(float celsius)
{
   if(Kchoice == \'Y\' || Kchoice == \'y\')
   {
       if(celsius < 0)
       {
           cout<<\"This temperature is not possible, as it is below 0 K\ \";
       }
       else
       {
           cout<<\"This is also a temperature of: \"<<(celsius + 273.15)<<\" K\"<<endl;
       }
   }
}

//method to convert fahrenheit to celsius
void FtoC()
{
   float temp = getTemp();
   float temperature = (temp-32) * 5/9;
   cout<<\"\ A temp of \"<<temp<<\" Fahrenheit converted to Celsius = \"<<temperature<<\" C\"<<endl;
   showDegreesK(temperature);
}
//method to convert celsius to fahrenheit
void CtoF()
{
   float temp = getTemp();
   float temperature = temp*9/5 + 32;
   cout<<\"\ A temp of \"<<temp<<\" Celsius converted to Fahrenheit = \"<<temperature<<\" F\"<<endl;
   showDegreesK(temp);
}

//main method
int main()
{

   int menuChoice;
   cout<<\"Welcome to the Temperature Converter!\"<<endl;
   cout<<\"Would you also like to see degrees Kelvin (K) in the results? (Y/N): \";;
   cin>>Kchoice;
   cout<<\"\ Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): \";
   try
   {
       menuChoice = getCType();
       while(menuChoice != 0)
       {
           switch(menuChoice)
           {
           case 1: cout<<\"\ Enter your Fahrenheit temperature: \";
                   FtoC();
                   break;

           case 2: cout<<\"\ Enter your Celsius temperature: \";
                   CtoF();
                   break;
           case 3: cout<<\"\ Would you also like to see degrees Kelvin (K) in the results? (Y/N): \";
                   cin>>Kchoice;
                   break;
           }
           cout<<\"\ Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): \";
           menuChoice = getCType();
       }
   }catch(const char* msg)
   {
       cout << msg << endl;
   }
   return 0;
}

OUTPUT:

Welcome to the Temperature Converter!

Would you also like to see degrees Kelvin (K) in the results? (Y/N): Y

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1

Enter your Fahrenheit temperature: 32

A temp of 32 Fahrenheit converted to Celsius = 0 C

This is also a temperature of: 273.15 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2

Enter your Celsius temperature: 100

A temp of 100 Celsius converted to Fahrenheit = 212 F

This is also a temperature of: 373.15 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 2

Enter your Celsius temperature: -300

A temp of -300 Celsius converted to Fahrenheit = -508 F

This temperature is not possible, as it is below 0 K

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 3

Would you also like to see degrees Kelvin (K) in the results? (Y/N): N

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 1

Enter your Fahrenheit temperature: -40

A temp of -40 Fahrenheit converted to Celsius = -40 C

Select conversion type (1=F to C, 2=C to F, 3=Stop/Start K, 0=end): 0

Midterm: Temp Converter (50 points) 2016 This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An
Midterm: Temp Converter (50 points) 2016 This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An
Midterm: Temp Converter (50 points) 2016 This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An
Midterm: Temp Converter (50 points) 2016 This assignment is a simple temperature converter, which can convert Fahrenheit to Celsius or Celsius to Fahrenheit. An

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site