Reputation: 1
I have been trying to write a temperature conversion program to teach myself how the class system in c++ which will convert any given temperature input (Celsius, Fahrenheit and kelvin) to the other two values then output all three values again. However I am unable to get my project to compile and I am too much of a rookie to find the errors myself. (Believe me I have tried all day). If someone could please take a look at it and make any suggestions/improvements I would greatly appreciate it. Apologies for not writing any comments into my code by the way.
#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;
}
}
}
Compilation errors are as follows:
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
Upvotes: 0
Views: 1987
Reputation: 4873
You had many problems with your code:
static
(not really a problem, but warns with C++03)setTemperature()
.main()
outside the class.int main()
to int main(void)
.char temperatureformat = 0
instead of char temperatureformat = NULL
., setTemperature
to .setTemperature
.main()
.All of them are comments prefixed by 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;
}
Upvotes: 0
Reputation: 3576
Add some functions definitions to your class in addition with ;
at end of 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;
}
Upvotes: 0
Reputation: 289
This line, maybe?
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
I'm not sure what you're trying to do with that comma there. You probably want to put a period instead. You also need to put semicolons at the end of your class (after the last curly bracket). And finally, you should put your main method at the end of the file, outside of the class.
Upvotes: 1
Reputation: 16935
There are a few problems:
Unlike java, main
will be separate from the class.
You were missing get
methods that you were trying to use.
You had several typos.
I'm not sure if you actually want private constants. Macros would make more sense.
Anyway, here's the code (that compiles). It's untested, so that's left as an exercise for you.
#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;
}
}
Upvotes: 0
Reputation: 11
The line
if (temperatureCalculator, setTemperature (InputReading, temperatureformat))
Seems suspicious to me. Why is there a comma between temperatureCalculator
and setTemperature
? Did you mean to have a 'dot' (.
) to instead call the class method setTemperature
on your Temperature
class instance temperatureCalculator
?
If you post the actual error, we can help more. Good luck!
Upvotes: 1