Help with dynamic variables



 DEVELOP > c-Plus-Plus > Help with dynamic variables

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Tommy Lang"
Date: 23 Nov 2003 05:09:10 AM
Object: Help with dynamic variables
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);
}
//---------------------------------------------------------------
.

User: "Arne Stockman"

Title: Re: Help with dynamic variables 23 Nov 2003 09:41:22 AM
"Tommy Lang" <mums_se@yahoo.se> skrev i meddelandet
news:78dde37d.0311230309.12c03183@posting.google.com...

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;
}
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER