Hi, I am reading textfiles in a structure like this:

ifstream TextFile;
 TextFile.open("quote.txt");
 while (!TextFile.eof())
 {
  TextFile >> Quote[RecordNumber].RefNumber;
  TextFile >> Quote[RecordNumber].Surname;
  TextFile >> Quote[RecordNumber].Initials;
  TextFile >> Quote[RecordNumber].TotalCost;
  TextFile >> Quote[RecordNumber].DeliveryCost;
  TextFile >> Quote[RecordNumber].Guarantee;
  TextFile >> ws;
  RecordNumber++;
 }
 TextFile.close();

However, this needs to be put into a function. I tried to do it by doing this: (the structure is defined as RecDetails Quote)

RecDetails ReadRecords (RecDetails Quote, int RecordNumber=0)
{
 ifstream TextFile;
 TextFile.open("quote.txt");
 while (!TextFile.eof())
 {
  TextFile >> Quote[RecordNumber].RefNumber;
  TextFile >> Quote[RecordNumber].Surname;
  TextFile >> Quote[RecordNumber].Initials;
  TextFile >> Quote[RecordNumber].TotalCost;
  TextFile >> Quote[RecordNumber].DeliveryCost;
  TextFile >> Quote[RecordNumber].Guarantee;
  TextFile >> ws;
  RecordNumber++;
 }
 TextFile.close();
}

However i get an error message saying "operator+ not implemented in type 'RecDetails' for arguments of type 'int'"

Any ideas what im doing wrong?

Recommended Answers

All 3 Replies

Hi, I am reading textfiles in a structure like this:

ifstream TextFile;
 TextFile.open("quote.txt");
 while (!TextFile.eof())
 {
  TextFile >> Quote[RecordNumber].RefNumber;
  TextFile >> Quote[RecordNumber].Surname;
  TextFile >> Quote[RecordNumber].Initials;
  TextFile >> Quote[RecordNumber].TotalCost;
  TextFile >> Quote[RecordNumber].DeliveryCost;
  TextFile >> Quote[RecordNumber].Guarantee;
  TextFile >> ws;
  RecordNumber++;
 }
 TextFile.close();

However, this needs to be put into a function. I tried to do it by doing this: (the structure is defined as RecDetails Quote)

RecDetails ReadRecords (RecDetails Quote, int RecordNumber=0)
{
 ifstream TextFile;
 TextFile.open("quote.txt");
 while (!TextFile.eof())
 {
  TextFile >> Quote[RecordNumber].RefNumber;
  TextFile >> Quote[RecordNumber].Surname;
  TextFile >> Quote[RecordNumber].Initials;
  TextFile >> Quote[RecordNumber].TotalCost;
  TextFile >> Quote[RecordNumber].DeliveryCost;
  TextFile >> Quote[RecordNumber].Guarantee;
  TextFile >> ws;
  RecordNumber++;
 }
 TextFile.close();
}

However i get an error message saying "operator+ not implemented in type 'RecDetails' for arguments of type 'int'"

Any ideas what im doing wrong?

The function doesn't return anything, so you need to use "void". Second, Quote will terminate once the function exits which you can extract later so you'll need to pass by reference or via pointers. I've modified the code to this:

void ReadRecords (RecDetails *Quote, int RecordNumber)
{
ifstream TextFile("quote.txt");
RecordNumber = 0;
if (TextFile.is_open) {
cout << "Error opening file quote.txt" << endl;
} else {
while (!TextFile.eof())
{
TextFile >> Quote[RecordNumber]->RefNumber;
TextFile >> Quote[RecordNumber]->Surname;
TextFile >> Quote[RecordNumber]->Initials;
TextFile >> Quote[RecordNumber]->TotalCost;
TextFile >> Quote[RecordNumber]->DeliveryCost;
TextFile >> Quote[RecordNumber]->Guarantee;
/* TextFile >> ws; - ws is not declared in the function */
RecordNumber++;
} 
TextFile.close();
}
}

Good luck, LamaBot

Also, " while (!TextFile.eof()) " will cause your program to 'fail' at the end of file. Here's why [.eof() == feof()]

Thanks for the help guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.