-
February 25th, 2004, 10:49 PM
#1
storing and printing doubles
Below program was originally meant to receive any type of data, store them and then print them on the console. In the function add() the value to be stored, stored as one byte a time.
As an exercise I have to change it a bit to store 25 double values and then print them. But now it’s not functioning correctly. After storing the 25 numbers all I get is a bunch of zeros as the output at the console. Please could you give me a hint about what’s the wrong.
Code:
// implementation file
const int increment = 100;
void Stash::initialize(int sz)
{
size = sz;
quantity = 0;
storage = 0;
next = 0;
}
void Stash::add(const void* element)
{
if(next>=quantity)
inflate(increment);
int startBytes = next*size;
unsigned char* e = (unsigned char*) element;
for(int i=0; i<size; i++){
storage[startBytes+i] = e[i];
}
next++;
}
void Stash::print()
{
for(int i=0; i<=25; i++)
cout<<(double)storage[i*size]<<endl;
}
void Stash::inflate(int increase)
{
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char [newBytes];
for(int i=0; i<oldBytes; i++)
b[i] = storage[i];
delete []storage;
storage = b;
quantity = newQuantity;
}
// main method
void main()
{
Stash s;
s.initialize(sizeof(double));
for(double d=0; d<=25; d++)
s.add(&d);
s.print();
}
Thanks a lot for reading.
-
February 26th, 2004, 12:45 AM
#2
where are you allocating storage?
post the class definition .h file so we can run it..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|