Problem with storing and printing

This is a discussion on Problem with storing and printing within the C++ Programming forums, part of the General Programming Boards category; Please point out why below code is not working properly. The program is meant to store 25 doubles and print ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Problem with storing and printing

    Please point out why below code is not working properly. The program is meant to store 25 doubles and print them out again to the console.
    Code:
    // function add() of struct Stash, which adds values to storage
     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++;
    	}
    
    // function print(), which prints the values in storage to the console
    void Stash::print()
    	{
    	for(int i=0; i<=25; i++)
    	cout<<(double)storage[i*size]<<endl;
    	}
    
    // function inflate(), which adds more memory to storage
    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;
    	}
    
    // we call those functions from main() like this
    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.
    Last edited by kasun; 02-25-2004 at 08:04 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Exactly what is not working? You didn't post all functions (initialize for example) and you're making lots of casts to ints and void pointers though you're handling doubles... Since this is C++ perhaps you should make a template stash instead (if you know templates that is).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    I’m so sorry… my mistake.

    Actually the code is meant to store the double value byte-by-byte in the function add. The print function is supposed to print all the values in storage, but it doesn’t do that right. All I get is a bunch of zeros as the stored values is storage.

    Here is the initialize function:

    Code:
    void Stash::initialize(int sz) {
      size = sz;
      quantity = 0;
      storage = 0;
      next = 0;
    }
    And yes that cast in print should be to double.

    Thanks a lot for you interest.
    Last edited by kasun; 02-25-2004 at 08:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening a file and then printing the output?
    By Sholcomb1 in forum C Programming
    Replies: 5
    Last Post: 11-02-2006, 08:44 AM
  2. Storing and printing matrices
    By stevedawg85 in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 07:57 AM
  3. Help with Printing BMPs
    By simtron in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 09:21 AM
  4. Storing in structure
    By bennyboy in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 05:14 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21