446,227 Members | 1,373 Online
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

# Help with dynamic variables

 P: n/a 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 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); } //--------------------------------------------------------------- Jul 22 '05 #1