T
Tommy Lang
I am trying to learn to use dynamic variables.
I have pasted the code below.
Is this the proper way of using dynamic variables?
Thanks,
Tommy
//------------------------------------------------------------
#include <iostream>
using namespace std;
class Temp{
private:
int *temp;
public:
Temp();
~Temp();
void FahrenheitToCelsius();
void CelsiusToFahrenheit();
};
Temp::Temp()
{
cout << "Temp::Temp()" << endl;
}
Temp::~Temp()
{
delete temp;
}
void Temp::FahrenheitToCelsius()
{
// Initalize the variables as floats
int *degree;
// User input
cout << "Enter a degree to convert (Fahrenheit): ";
cin >> *degree;
temp = new int(*degree);
*temp = (5.0/9.0)*((*degree)-32.0);
// Output
cout << "Celsius degree is: " << (*temp) << endl << endl;
}
void Temp::CelsiusToFahrenheit()
{
// Initalize the variables as floats
int *degree;
// User input
cout << "Enter a degree to convert (Celsius): ";
cin >> *degree;
temp = new int(*degree);
*temp = (9.0*((*degree)/5.0) + 32.0);
// Output
cout << "Fahrenheit degree is: " << (*temp) << endl << endl;
}
void main()
{
// Initialize variables
Temp t;
int choice;
int end;
// User Menu and Input
do{
//Menu
cout << "Choose a conversion" << endl;
cout << "-------------------" << endl;
cout << "1) Celsius To Fahrenheit" << endl;
cout << "2) Fahrenheit To Celsius" << endl;
cout << "3) Exit" << endl << endl;
cin >> choice;
switch(choice)
{
case 1:
t.CelsiusToFahrenheit();
break;
case 2:
t.FahrenheitToCelsius();
break;
case 3:
cout << endl << endl << "Goodbye!";
end = 1;
break;
default:
cout << "Try again..." << endl << endl;
break;
}
} while(end!=1);
}
//---------------------------------------------------------------
I have pasted the code below.
Is this the proper way of using dynamic variables?
Thanks,
Tommy
//------------------------------------------------------------
#include <iostream>
using namespace std;
class Temp{
private:
int *temp;
public:
Temp();
~Temp();
void FahrenheitToCelsius();
void CelsiusToFahrenheit();
};
Temp::Temp()
{
cout << "Temp::Temp()" << endl;
}
Temp::~Temp()
{
delete temp;
}
void Temp::FahrenheitToCelsius()
{
// Initalize the variables as floats
int *degree;
// User input
cout << "Enter a degree to convert (Fahrenheit): ";
cin >> *degree;
temp = new int(*degree);
*temp = (5.0/9.0)*((*degree)-32.0);
// Output
cout << "Celsius degree is: " << (*temp) << endl << endl;
}
void Temp::CelsiusToFahrenheit()
{
// Initalize the variables as floats
int *degree;
// User input
cout << "Enter a degree to convert (Celsius): ";
cin >> *degree;
temp = new int(*degree);
*temp = (9.0*((*degree)/5.0) + 32.0);
// Output
cout << "Fahrenheit degree is: " << (*temp) << endl << endl;
}
void main()
{
// Initialize variables
Temp t;
int choice;
int end;
// User Menu and Input
do{
//Menu
cout << "Choose a conversion" << endl;
cout << "-------------------" << endl;
cout << "1) Celsius To Fahrenheit" << endl;
cout << "2) Fahrenheit To Celsius" << endl;
cout << "3) Exit" << endl << endl;
cin >> choice;
switch(choice)
{
case 1:
t.CelsiusToFahrenheit();
break;
case 2:
t.FahrenheitToCelsius();
break;
case 3:
cout << endl << endl << "Goodbye!";
end = 1;
break;
default:
cout << "Try again..." << endl << endl;
break;
}
} while(end!=1);
}
//---------------------------------------------------------------