Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3873157 • Letter: #

Question

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

// Declare Variables


int main()
{


// Programs Greetings
cout << "This program will calculate and convert temperatures for you. ";

Problem Your friend has asked you to help them with a project. He needs to convert a Fahrenheit temperature to either Celsius, Kelvin or Rankine temperature scales. However, your friend is a bit - odd. He needs this to apply to only three objects: a cat, a cot and a cap. Each object has a valid temperature range for his calculations. We only want to accept valid words cat, cap, cot no matter how they are spelled (ie Cat, CAT, caT all Ok too). Prompt for the object to calculate the temperature for. Ask for the temperature of the object in Fahrenheit. Check to see if it's in the proper range. · Create a menu for the temperature scale to convert to. Have an All option. Allowable Temp (F) for Objects Object Low Temp High Temp cat cot cap 86.0 54.0 72.0 102.0 80.0 88.0 Constraints Make sure the user can input the temperatures only within a valid . If out of range, give them information (too high, too low) and . Temperatures must be valid floats with in the proper range. We . Restrict output to 3 significant digits, always. Have the tempera- range. reprompt can accept an error of less than o.2 degrees. tures line up on the decimals.

Explanation / Answer

#include <iostream>
#include <cmath>
#include <iomanip>
#include <strings.h>
using namespace std;


int main()
{
char animal[5]; // animal is string type of length 5,
double temp_in_f; // temperature in fharenheit
double temp_in_c; // temperature in celsius
double temp_in_k; // tempeature in kelvin
double temp;
int flag;

cout << "Enter animal (cat/cot/cap) : ";
cin >> animal;
if ( ! (strcasecmp(animal,"cat") == 0 || strcasecmp(animal,"cot") == 0 || strcasecmp(animal,"cap") == 0 ) )
   {
     cout << "please kindly enter animal (cat/cot/cap) " << endl;
     return -1;
   }
while(1)
   {
     cout << "Enter temperature in Fharenheit : ";
     cin >> temp_in_f;

     flag = 0;
   
     if( strcasecmp(animal,"cat") == 0 )
       {
         temp = temp_in_f - 85.8; // 0.2 degrees temperature correction
         if ( temp == 0.0f) flag=0;
        else if(temp_in_f<86.0f )
              flag=1;
         else if ( temp_in_f>102.0f )
             flag=2;
      
        }
     else if( strcasecmp(animal,"cot") == 0 )
       {
         temp = temp_in_f - 53.8; // 0.2 degrees temperature correction
         if ( temp == 0.0f) flag=0;
         else if(temp_in_f<54.0f)
             flag=1;
         else if ( temp_in_f>180.0f)
             flag=2;
       }
     else if( strcasecmp(animal,"cap") == 0 )
       {
         temp = temp_in_f - 71.8; // 0.2 degrees temperature correction
         if ( temp == 0.0f) flag=0;
         else if(temp_in_f<72.0f)
             flag=1;
         else if ( temp_in_f>88.0f)
             flag=2;
       }
     if(flag==0)
        break;
     else if (flag==1)
       {
         cout << "Temperature is too low... " << endl;
         cout << "Please kindly re-input... " << endl;
         continue;
       }
     else if ( flag==2)
       {
         cout << "Temperature is too high... " << endl;
cout << "Please kindly re-input... " << endl;
         continue;
       }
    }
  
   cout << "Enter C / K [ C - Celsius, K - Kelvin ] : ";
   char ch; // menu choice
   cin >> ch;
   if ( ch=='C' || ch=='c')
    {
      temp_in_c = (temp_in_f - 32) * ((double)5/9);
      cout << setw(3) << setprecision(3)<< temp_in_c << endl;
    }
   else if (ch=='K' || ch=='k' )
    {
      temp_in_k = ( temp_in_f + 459.67f ) * ((double)5/9);
      cout << setw(3) << setprecision(3)<< temp_in_k << endl;
    }
   else
    {
       cout << "invalid Temperature choice .... " << endl;
    }
return 0;
}

/*
Output:
lenovo@lenovo-Vbox:~/chegg$ g++ -Wall tmpcnv.cpp
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter animal (cat/cot/cap) : cit
please kindly enter animal (cat/cot/cap)
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter animal (cat/cot/cap) : cat
Enter temperature in Fharenheit : 23
Temperature is too low...

Please kindly re-input...

Enter temperature in Fharenheit : 103
Temperature is too high...

Please kindly re-input...

Enter temperature in Fharenheit : 85.7
Temperature is too low...

Please kindly re-input...

Enter temperature in Fharenheit : 85.8
Enter C / K [ C - Celsius, K - Kelvin ] : c
29.9
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter animal (cat/cot/cap) : cat
Enter temperature in Fharenheit : 102
Enter C / K [ C - Celsius, K - Kelvin ] : K
312
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter animal (cat/cot/cap) : cot
Enter temperature in Fharenheit : 34
Temperature is too low...

Please kindly re-input...

Enter temperature in Fharenheit : 53.8
Enter C / K [ C - Celsius, K - Kelvin ] : K
285
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter animal (cat/cot/cap) : cap
Enter temperature in Fharenheit : 71.8
Enter C / K [ C - Celsius, K - Kelvin ] : C
22.1
lenovo@lenovo-Vbox:~/chegg$
*/