By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
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 <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);
}
//---------------------------------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Tommy Lang" <mu*****@yahoo.se> skrev i meddelandet
news:78**************************@posting.google.c om...
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;
}
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.