# Dynamic Memory Alloctaion help

This is a discussion on Dynamic Memory Alloctaion help within the C++ Programming forums, part of the General Programming Boards category; Hello, Im having a problem with putting output from a file onto the screen. It compiles fine with no errors ...

1. ## 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. I believe you cannot do while(readOk), I think you can only do this will bool statements and not ints...

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

4. 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. 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