Thread: Dynamic Memory Alloctaion help

  1. #1
    Curwa
    Guest

    Dynamic Memory Alloctaion help

    Hello,

    Im having a problem with putting output from a file onto the screen. It compiles fine with no errors but when i run it, no output is shown on the screen. This is kinda new to me doing it like this. Did I do something wrong in the code because it seems fine? Any help would be much appreciated. Thanks in advance!

    Code:
    salesData* loadArray (ifstream& fsInventoryFile)
    {
       salesData* headPointer;
       salesData* currentPointer;
       salesData record;
       int readOk;
       readOk = readARec(fsInventoryFile, record); //this function is fine
       while(readOk)
       {
          currentPointer = new salesData;
          headPointer = currentPointer;
          *(currentPointer)=record;
          (currentPointer)->next=headPointer;
          readOk = readARec(fsInventoryFile, record);
       }
        cout <<"loadArray"<<endl;
        return currentPointer;
    }
    
    
    void displayArray (salesData* headPointer)
    {
        int i=0;
        salesData* currentPointer = headPointer;
       while (currentPointer != NULL)
       {
          cout <<i+1 <<")\t";
          cout <<setw(9) <<currentPointer->itemCode
                  <<setw(17) <<&(currentPointer->itemCode);
         cout.unsetf(ios::left);
         cout <<setw(2) <<currentPointer->quantity;
         cout.setf(ios::left);
        cout <<setw(4) << " " 
                <<setw(14) <<&(currentPointer->quantity)
                <<setw(8) <<currentPointer->price
                <<&(currentPointer->price)
                <<endl;
                 currentPointer = currentPointer->next;
    	i++;
    	}
    
    	cout <<endl <<endl;
    
    	return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I believe you cannot do while(readOk), I think you can only do this will bool statements and not ints...

  3. #3
    Curwa
    Guest

    Smile

    I tried that...nothing happened. But thanks for the thought...

  4. #4
    Curwa
    Guest

    Talking

    I'll post the rest of the code just in case it is needed....

    Code:
    struct salesData
    {
    	char itemCode[8];
    	int quantity;
    	float price;
    	//-------------
    	salesData *next;	//record pointer
    };
    
    salesData* loadArray (ifstream& fsInventoryFile);
    void displayArray (salesData* headPointer);
    int readARec(ifstream& fsInventoryFile,salesData& record);
    
    const int SIZE = 100;
    
    void main (void)
    {
      ifstream fsInventoryFile;
      char filePath[80] = "a:\\in05.01";
      salesData *headPointer=NULL;
      salesData *currentPointer=NULL;
      headPointer=currentPointer;
      
      fsInventoryFile.open(filePath);
       if(!fsInventoryFile)
       {
    	cout <<"Unable to open: " <<filePath <<endl;
       }
       else
       {
         headPointer = loadArray (fsInventoryFile);
         displayArray (headPointer);
         fsInventoryFile.close();
        }
    
    }// end of main
    
    // code from above post here
    
    int readARec(ifstream& fsInventoryFile, salesData& record)
    {
       bool readOk = 1;
       fsInventoryFile >> record.quantity
    	                >> record.price
    	                >> record.itemCode;
       if ((fsInventoryFile.eof()) || (fsInventoryFile.fail()))
    	{
    	readOk=0;
    	}
    
       return readOk;
    }//end readARec

  5. #5
    Curwa
    Guest

    Smile

    Thanks for your help.... I found out it is suppose to go backwards as a link list... i will try and play around with it to see if i can make it work as it is suppose too...or any help is always nice...Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy .dat file to dynamic memory...
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 04:36 PM
  2. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM