问题
I am having an issue with my current programming assignment. I feel as though I am very close to having it correct, but something is off. I know that I have to do something different in order to make the program function correctly, as it doesn't work right now, but I am not sure what it is.
I am struggling specifically with how to use a single private variable to make both kinds of temperatures.
Here is the assignment:
Make a Temperature class. The class should have a function for setting the temperature in Fahrenheit and function for setting a temperature in Celsius. Keep only one data member in the private section for storing the temperature. Create a function for obtaining the in Fahrenheit and a function for obtaining the temperature in Celsius. Test each function thoroughly with a driver.
F = (9/5)C + 32, C = (5/9)(F - 32)
Current Code:
#include<iostream>
using namespace std;
class Temperature
{
private:
double temperature;
public:
void set_fahrenheit(double f)
{
temperature = f;
}
void set_celsius(double c)
{
temperature = c;
}
double get_fahrenheit()
{
return temperature;
}
double get_celsius()
{
return temperature;
}
double converter(double temperature)
{
if (temperature = f)
{
return (9/5)*temperature + 32;
}
else if (temperature = c))
{
return (5/9)*(temperature - 32;
}
}
};
int main()
{
Temperature Temp1;
double temperaturetemp;
string response;
cout << "Would you like to convert a Celsius temperature to Fahrenheit or convert a Fahrenheit temperature to Celsius? (Enter C2F or F2C respectively)" << endl;
cin >> response;
cout << "Please enter the temperature you would like to convert in degrees" << endl;
cin >> temperaturetemp;
if (response == "C2F"){Temp1.set_fahrenheit(temperaturetemp);}
else if (response == "F2C"){Temp1.set_celsius(temperaturetemp);}
cout << Temp1.converter(temperaturetemp);
}
回答1:
Just use one specific unit to store the temperature internally, best would be Kelvin1 IMO (as it's the standard SI physical unit).
Do the necessary calculations when setting or getting the temperature as Fahrenheit or degrees Celsius.
Also don't use integer division to represent fractions:(5/9)
will result as 0
integer division, it should be (5.0/9.0)
to get a valid double
value.
Same with 9/5
as integer division will result as 1
.
Further issues with your code:
- In your function
double converter(double temperature)
you try to usef
, which isn't in scope there - In that same function you have a parameter named
temperature
, which shadows your member variable with the same name
1)0K = -459,67F / 0K = -273.15°C
回答2:
Your issue is because you are trying to compare the temperature value to the initial variable name in order to determine if it is Fahrenheit or Celsius.
if (temperature = f)
...
else if (temperature = c))
You should choose one temperature type over the other to always store the value as and convert for the other when needed. In this example Celsius is used.
void set_celsius(double c)
{
temperature = c;
}
void set_fahrenheit(double f)
{
temperature = (5.0/9.0)(f - 32);
}
The same can be done with your getter for Fahrenheit. Your converter method really isn't needed (and not being called atm).
EDIT
You should also be using floating point math as integer math will be truncated to 0 since the value you are after is a decimal, 0.5555...
Storing the value as one of the desired temperatures will save on calculations when that temperature type is needed. In this code, it wouldn't make a difference but when scaling software up it's important to eliminate excessive processing.
来源:https://stackoverflow.com/questions/52505069/question-regarding-using-only-setters-and-variables-correctly