Help with dynamic variables

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);
}
//---------------------------------------------------------------
 
A

Arne Stockman

Tommy Lang said:
I am trying to learn to use dynamic variables.
I have pasted the code below.
Is this the proper way of using dynamic variables?

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);

The pointer degree is uninitialized, cin >> *degree will write to an
undetermined spot in memory, possibly overwriting valid data or casing the
program to crash. This example is also quite meaningless for using dynamic
memory, since the main reason to use dynamic memory is that you dont know
the size of the memory you need. Here you are allocating a single int
variable, wich you really not need at all. I would also initialize the temp
variable to NULL in the constructor.

void Temp::CelsiusToFahrenheit()
{
// Initalize the variables as floats
double degree;
// User input
cout << "Enter a degree to convert (Celsius): ";
cin >> degree;
degree = (9.0 * degree / 5.0 + 32.0);
// Output
cout << "Fahrenheit degree is: " << (int)degree << endl << endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top