#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. ";
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$
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.