Fahrenheit To Celsius

Nov 24, 2021 at 10:17pm
You are to write a program that converts a Fahrenheit temperature to Celsius. The design of this program is important as I want you to break your program into separate functions and have the main() function call these to do the work. We will discuss in class in more detail.

Function prototypes should be similar to the ones shown below:


double getfahrenheit();
double converttocelsius(double);
void displaytable(double,double);






#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
using namespace std;

void displaytable(double,double);
double getfahrenheit();
double converttocelsius(double);

int main()
{
double fahrenheit;
double celsius;


cout << fixed << showpoint << setprecision(1);

displaytable(fahrenheit, celsius);

fahrenheit = getfahrenheit();
celsius = converttocelsius(fahrenheit);

celsius = 5 * (fahrenheit - 32) / 9;


do
{
displaytable(fahrenheit, celsius);
cin >> fahrenheit;
}
while (fahrenheit > -55 || fahrenheit < 125);
{
cout << "Please enter Fahrenheit Temperature. Number should be between -55 to 125 ";
cin >> fahrenheit;
}

return 0;
}
double getfahrenheit()
{
double fahrenheit;
cout << "Please enter Fahrenheit Temperature. Number should be between -55 to 125 ";
cin >> fahrenheit;
return fahrenheit;
}

void displaytable(double Fahrenheit, double celsius)
{
cout << "Please enter Fahrenheit Temperature. Number should be between -55 to 125 "<<endl;
cout << "Fahrenheit\t Celsius"<<endl;
cout << "-----------------------\n";
cout << Fahrenheit << " \t" << celsius<< endl;
}
double converttocelsius(double fahrenheit)
{
double celsius;

celsius = 5.0 * (fahrenheit - 32) / 9.0;
return fahrenheit;
}






Nov 25, 2021 at 12:15am
You forgot to ask a question.

There's more than one suspicious thing in your code, but as a start:

1
2
3
4
double fahrenheit;
double celsius;
// ...
displaytable(fahrenheit, celsius);

You're not initializing your fahrenheit or celsius variables, but you're passing them (by value) into a function. Why?

Look at what you're returning in your converttocelsius function.
1
2
celsius = 5.0 * (fahrenheit - 32) / 9.0;
return fahrenheit;
Nov 25, 2021 at 10:21am
Nov 25, 2021 at 12:19pm
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.