温度转换程序c++
Temperature conversion program c++
我一直在尝试编写一个温度转换程序来自学 c++ 中的 class 系统如何将任何给定的温度输入(摄氏度、华氏度和开尔文)转换为其他两个值,然后输出所有再次三个值。但是我无法让我的项目编译,而且我是一个新手,无法自己发现错误。 (相信我,我已经试了一整天了)。
如果有人可以看一下并制作 suggestions/improvements ,我将不胜感激。
顺便说一句,很抱歉没有在我的代码中写任何评论。
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
const double ConversionRate = 0.55555;
const double AbsoluteZero = 273.15;
const int TemperatureOffset = 32;
double InputTemperature = 0;
double Fahrenheit = 0, Celsius = 0, Kelvin = 0;
public:
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
}
int main()
{
double InputReading = 0;
Temperature temperatureCalculator;
char temperatureformat = NULL;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
}
}
编译错误如下:
Severity Code Description Project File Line
Error (active) class "Temperature" has no member "getCelsius" 69
Error (active) class "Temperature" has no member "getFahrenheit" 70
Error (active) class "Temperature" has no member "getKelvin" 71
Error (active) expected a ';' 77
Error C1004 unexpected end-of-file found 78
行
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
我觉得很可疑。为什么 temperatureCalculator
和 setTemperature
之间有一个逗号?您的意思是要使用 'dot' (.
) 来代替在您的 Temperature
class 实例 [=11= 上调用 class 方法 setTemperature
]?
如果您 post 实际错误,我们可以提供更多帮助。祝你好运!
也许是这条线?
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
我不确定你想用那个逗号做什么。您可能想用句号代替。您还需要在 class 的末尾加上分号(在最后一个大括号之后)。最后,您应该将 main 方法放在文件的末尾,在 class.
之外
有几个问题:
与java不同,main
将与class分开。
您缺少 get
您尝试使用的方法。
你有几个错别字。
我不确定您是否真的需要私有常量。宏会更有意义。
无论如何,这是代码(可以编译)。它未经测试,因此留给您作为练习。
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
const double ConversionRate = 0.55555;
const double AbsoluteZero = 273.15;
const int TemperatureOffset = 32;
double InputTemperature = 0;
double Fahrenheit = 0, Celsius = 0, Kelvin = 0;
public:
double getFahrenheit() { return Fahrenheit; }
double getCelsius() { return Celsius; }
double getKelvin() { return Kelvin; }
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
}
};
int main()
{
double InputReading = 0;
Temperature temperatureCalculator;
char temperatureformat = NULL;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin() << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
}
您的代码有很多问题:
- 将转换常量更改为
static
(不是真正的问题,但会在 C++03 中发出警告)
- 在构造函数中初始化局部变量,而不是在 class 定义中。 (在 C++03 中警告)
- 为局部变量定义 getter。
- Return 来自
setTemperature()
. 的值
- 将
main()
的定义移到 class 之外。
- 在 class 定义后添加一个分号。
- 将
int main()
更改为 int main(void)
。
- 使用
char temperatureformat = 0
代替 char temperatureformat = NULL
。
- 将
, setTemperature
更改为 .setTemperature
。
- 在函数名称后添加括号(必需)。
- 为
main()
添加 return 值。
都是CHANGE:
前缀的评论
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
// CHANGE: DECLARE STATIC
static const double ConversionRate = 0.55555;
static const double AbsoluteZero = 273.15;
static const int TemperatureOffset = 32;
// CHANGE: USE CONSTRUCTOR BELOW INSTEAD FOR INITIALIZATION
double InputTemperature;
double Fahrenheit, Celsius, Kelvin;
public:
// CHANGE: DEFINE CONSTRUCTOR
Temperature() : InputTemperature(0), Fahrenheit(0), Celsius(0), Kelvin(0) {}
// CHANGE: DEFINE GETTERS
double getFahrenheit() const {return Fahrenheit;}
double getCelsius() const {return Celsius;}
double getKelvin() const {return Kelvin;}
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
// CHANGE: RETURN VALUE
return temperatureConfigured;
}
// CHANGE: MOVE main() OUTSIDE CLASS
}; // CHANGE: ADD SEMICOLON AFTER CLASS
// CHANGE: MAKE int main(void)
int main(void)
{
double InputReading = 0;
Temperature temperatureCalculator;
// CHANGE: MAKE 0, NOT NULL
char temperatureformat = 0;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
// CHANGE: USE . MEMBER ACCESS OPERATOR
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
// CHANGE: ADD PARENTHESES AFTER FUNCTION NAMES
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin() << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
// CHANGE: RETURN VALUE
return 0;
}
除了 class 末尾的 ;
外,还向 class 添加一些函数定义:
Error (active) class "Temperature" has no member "getCelsius" 69
double getCelsius()
{
return Celsius;
}
Error (active) class "Temperature" has no member "getFahrenheit" 70
double getFahrenheit()
{
return Fahrenheit;
}
Error (active) class "Temperature" has no member "getKelvin" 71
double getKelvin()
{
return Kelvin;
}
我一直在尝试编写一个温度转换程序来自学 c++ 中的 class 系统如何将任何给定的温度输入(摄氏度、华氏度和开尔文)转换为其他两个值,然后输出所有再次三个值。但是我无法让我的项目编译,而且我是一个新手,无法自己发现错误。 (相信我,我已经试了一整天了)。 如果有人可以看一下并制作 suggestions/improvements ,我将不胜感激。 顺便说一句,很抱歉没有在我的代码中写任何评论。
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
const double ConversionRate = 0.55555;
const double AbsoluteZero = 273.15;
const int TemperatureOffset = 32;
double InputTemperature = 0;
double Fahrenheit = 0, Celsius = 0, Kelvin = 0;
public:
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
}
int main()
{
double InputReading = 0;
Temperature temperatureCalculator;
char temperatureformat = NULL;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
}
}
编译错误如下:
Severity Code Description Project File Line
Error (active) class "Temperature" has no member "getCelsius" 69
Error (active) class "Temperature" has no member "getFahrenheit" 70
Error (active) class "Temperature" has no member "getKelvin" 71
Error (active) expected a ';' 77
Error C1004 unexpected end-of-file found 78
行
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
我觉得很可疑。为什么 temperatureCalculator
和 setTemperature
之间有一个逗号?您的意思是要使用 'dot' (.
) 来代替在您的 Temperature
class 实例 [=11= 上调用 class 方法 setTemperature
]?
如果您 post 实际错误,我们可以提供更多帮助。祝你好运!
也许是这条线?
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
我不确定你想用那个逗号做什么。您可能想用句号代替。您还需要在 class 的末尾加上分号(在最后一个大括号之后)。最后,您应该将 main 方法放在文件的末尾,在 class.
之外有几个问题:
与java不同,
main
将与class分开。您缺少
get
您尝试使用的方法。你有几个错别字。
我不确定您是否真的需要私有常量。宏会更有意义。
无论如何,这是代码(可以编译)。它未经测试,因此留给您作为练习。
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
const double ConversionRate = 0.55555;
const double AbsoluteZero = 273.15;
const int TemperatureOffset = 32;
double InputTemperature = 0;
double Fahrenheit = 0, Celsius = 0, Kelvin = 0;
public:
double getFahrenheit() { return Fahrenheit; }
double getCelsius() { return Celsius; }
double getKelvin() { return Kelvin; }
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
}
};
int main()
{
double InputReading = 0;
Temperature temperatureCalculator;
char temperatureformat = NULL;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin() << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
}
您的代码有很多问题:
- 将转换常量更改为
static
(不是真正的问题,但会在 C++03 中发出警告) - 在构造函数中初始化局部变量,而不是在 class 定义中。 (在 C++03 中警告)
- 为局部变量定义 getter。
- Return 来自
setTemperature()
. 的值
- 将
main()
的定义移到 class 之外。 - 在 class 定义后添加一个分号。
- 将
int main()
更改为int main(void)
。 - 使用
char temperatureformat = 0
代替char temperatureformat = NULL
。 - 将
, setTemperature
更改为.setTemperature
。 - 在函数名称后添加括号(必需)。
- 为
main()
添加 return 值。
都是CHANGE:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Temperature
{
private:
// CHANGE: DECLARE STATIC
static const double ConversionRate = 0.55555;
static const double AbsoluteZero = 273.15;
static const int TemperatureOffset = 32;
// CHANGE: USE CONSTRUCTOR BELOW INSTEAD FOR INITIALIZATION
double InputTemperature;
double Fahrenheit, Celsius, Kelvin;
public:
// CHANGE: DEFINE CONSTRUCTOR
Temperature() : InputTemperature(0), Fahrenheit(0), Celsius(0), Kelvin(0) {}
// CHANGE: DEFINE GETTERS
double getFahrenheit() const {return Fahrenheit;}
double getCelsius() const {return Celsius;}
double getKelvin() const {return Kelvin;}
bool setTemperature(double temperature, char temperatureformat)
{
bool temperatureConfigured = true;
if (temperatureformat == 'c') {
Celsius = temperature;
Fahrenheit = ((1 / ConversionRate)*(Celsius)) + TemperatureOffset;
Kelvin = Celsius + AbsoluteZero;
}
else if (temperatureformat == 'f') {
Fahrenheit = temperature;
Celsius = (ConversionRate*(Fahrenheit - TemperatureOffset));
Kelvin = (Celsius + AbsoluteZero);
}
else if (temperatureformat == 'k') {
if (temperature >= 0)
{
Kelvin = temperature;
Celsius = Kelvin - AbsoluteZero;
Fahrenheit = (1 / ConversionRate)*Celsius + TemperatureOffset;
}
else {
temperatureConfigured = false;
Celsius = 0;
Kelvin = 0;
Fahrenheit = 0;
}
}
else {
temperatureConfigured = false;
}
// CHANGE: RETURN VALUE
return temperatureConfigured;
}
// CHANGE: MOVE main() OUTSIDE CLASS
}; // CHANGE: ADD SEMICOLON AFTER CLASS
// CHANGE: MAKE int main(void)
int main(void)
{
double InputReading = 0;
Temperature temperatureCalculator;
// CHANGE: MAKE 0, NOT NULL
char temperatureformat = 0;
cout << "Please Enter your temperature value" << endl;
cin >> InputReading;
cout << "Celsius - c" << endl;
cout << "Fahrenheit - f" << endl;
cout << "Kelvin - k" << endl;
cin >> temperatureformat;
// CHANGE: USE . MEMBER ACCESS OPERATOR
if (temperatureCalculator.setTemperature (InputReading, temperatureformat)) {
cout << "Your temperature conversions are" << endl;
cout << "Celsius: " << temperatureCalculator.getCelsius() << endl;
// CHANGE: ADD PARENTHESES AFTER FUNCTION NAMES
cout << "Fahrenheit: " << temperatureCalculator.getFahrenheit() << endl;
cout << "Kelvin: " << temperatureCalculator.getKelvin() << endl;
}
else {
cout << "Error, your input was invalid" << endl;
}
// CHANGE: RETURN VALUE
return 0;
}
除了 class 末尾的 ;
外,还向 class 添加一些函数定义:
Error (active) class "Temperature" has no member "getCelsius" 69
double getCelsius()
{
return Celsius;
}
Error (active) class "Temperature" has no member "getFahrenheit" 70
double getFahrenheit()
{
return Fahrenheit;
}
Error (active) class "Temperature" has no member "getKelvin" 71
double getKelvin()
{
return Kelvin;
}