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.
Thanks a lot for reading.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(); }


Reply With Quote
Bookmarks