(C++) Write a function named tempConvert that accepts a floating point number re
ID: 3794580 • Letter: #
Question
(C++) Write a function named tempConvert that accepts a floating point number representing a temperature and a character. If the character passed to the function is the letter f, the function should convert the passed temperature from Fahrenheit to Celsius; otherwise, of the character passed to the function is the letter c, the function should convert the passed temperature from Celsius to Fahrenheit. Test your function on the template provided.
This code is already provided
#include<iostream>
#include<iomanip>
using namespace std;
float tempConvert(double, int ch);//write your function prototype here
int main()
{
cout << "Temperature converter" << endl ;
float t;
char ch;
cout << "Enter the temperature: ";
cin >> t;
cout << "Enter the character (f or c): ";
cin >> ch;
}//ask the user to enter the temperature to be converted
char faren(tempConvert)
//ask the user to enter the type of temperature F or C (the use may enter c or f as well)
//call the function
float tempConvert (float ftemp, int ch){//display the result
cout << fixed << setprecision(2) << endl ;
if (type == 'C' || type == 'c')
cout << "The Fahrenheit equivalente is: " << /*your variable here */ << endl ;
else if (type == 'F' || type == 'f')
cout << "The Celsius equivalente is: " << /*your variable here */ << endl ;
else
cout << "Wrong scale. Cannot perform the conversion" << endl ;
return 0;
}
//write your function definition here
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
float tempConvert(double val,char ch);//write your function prototype here
int main()
{
cout << "Temperature converter" << endl ;
float val;
char ch;
cout << "Enter the temperature: ";
cin >> val;
cout << "Enter the character (f or c): ";
cin >> ch;
tempConvert(val,ch);
return 0;
}
float tempConvert(double temp,char type){//display the result
cout << fixed << setprecision(2) << endl ;
if (type == 'C' || type == 'c')
cout << "The Fahrenheit equivalente is: " <<((temp*1.8)+32.0)<< endl ;
else if (type == 'F' || type == 'f')
cout << "The Celsius equivalente is: " <<((temp-32.0)*5)/9<< endl ;
else
cout << "Wrong scale. Cannot perform the conversion" << endl ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.