473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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);
}
//---------------------------------------------------------------
Jul 22 '05 #1
1 2227

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John | last post by:
Hi all, I've got an issue with session variables in asp pages. I need to make a decision that I think you can hopefully help me out here. When a session starts on the iss server I need to...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
10
by: Bob Bedford | last post by:
In the attempt to keep the URL and code quite clean, and avoid to have a very loooong url, we have used $_session for storing values trough the pages. Now, we have some clients that doesn't get...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
12
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
4
by: Mr. Smith | last post by:
Hi. What's the correct syntax here: (given the recordset myRS with x records do until myRS.EOF txtvar & myRS("serial_number") = myRS("serial_name") myRS.movenext loop ..asp will not build...
28
by: Nutkin | last post by:
Basicly i have to make a program that calculates the distance between x and y points in 2d space. the code basicly goes like this 1. User says how many points they have (max of 10) 2. User...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.