storing and printing doubles
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: storing and printing doubles

  1. #1
    Join Date
    Feb 2004
    Location
    Far East
    Posts
    16

    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.

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center