void Load() 
{
	book *newptr,*p,*q;
	
	string id,title,author,year,price,date;
	ifstream file1;
	head = NULL;
	file1.open("books.txt");
    
	if (file1.fail())
	{
		cout << "Error opening file.\n";
		exit(1);
	}

	while (!file1.eof())
	{
		newptr = new book;  //
		
		getline(file1,id);
		getline(file1,title);
		getline(file1,author);
		getline(file1,year);
		getline(file1,price);
		getline(file1,date);

               //copy values from the file to the newptr		
		newptr->id.assign (id) ;
		newptr->title.assign (title) ;
		newptr->author.assign(author) ;
		newptr->year.assign (year) ;
		newptr->price = atoi(price.c_str()) ;
		newptr->date.assign (date) ;
		
		
		if(head == NULL)
		head = newptr;
                 else
                {
		p = head;
		q = NULL;
		while(p != NULL && p->id.compare(newptr->id)>0)
		{
			q = p;
			p = p->next;
		 
		}
		
		if(q == NULL)
	 	{
	 		newptr->next = head;
	 		head = newptr;
		
		}
		
		else
		{	
			newptr->next = p;
			q->next = newptr;
		
		}
		
		
		}
	}

	file1.close();

when i compile;
[Error] D:\C-Free Standard\temp\Untitled3.cpp:369: request for member `assign' in `newptr->book::id', which is of non-aggregate type `char[12]'
[Error] D:\C-Free Standard\temp\Untitled3.cpp:372: request for member `assign' in `newptr->book::year', which is of non-aggregate type `int'
[Error] D:\C-Free Standard\temp\Untitled3.cpp:374: request for member `assign' in `newptr->book::date', which is of non-aggregate type `char[10]'
[Error] D:\C-Free Standard\temp\Untitled3.cpp:384: request for member `compare' in `p->book::id', which is of non-aggregate type `char[12]'

Recommended Answers

All 6 Replies

Behold the dangers of a bad naming convention! :) You should consider changing the name of "id" within this function to something more like "incomingId" or "newId" so that you don't get it confused with book::id. This also helps prevent confusing your compiler (which is possible believe it or not).

You'll have to post the declaration of your "book" class so that we can see how book::id is defined. I'm guessing your book::id is declared as a C-style string (array of char) which does not have an assign() method instead of as a std::string object, which does.

Odds are you have the same issue with book::date. The issue with book::year is similar. You are probably trying to store a std::string object to an int, which isn't directly possible without some special conversion(s).

struct book declaration

struct book
{	
	char id [12];
	string title;
	string author;
	int year;
	float price;
	char date[10];
	struct book *next;
};

Are you simply trying to set the values?

Here is how you would set the value of newptr->title to that of title

newptr->title = title;

The rest are very similar. I suggest that since year and price inside your book object are not strings, that you actually make them an int and a float at the point of reading them in? i.e. instead of:

string id,title,author,year,price,date;

this:
string id,title,author,date;
int year;
float price;

and rather than read them in as strings, read them straight into the appropriate format.

still got error:

Compiling D:\C-Free Standard\temp\Untitled7.cpp...
[Error] D:\C-Free Standard\temp\Untitled7.cpp:372: no matching function for call to `getline (ifstream &, int &)'
[Error] D:\C-Free Standard\temp\Untitled7.cpp:373: no matching function for call to `getline (ifstream &, float &)'

Right, time to stop guessing how functions work and start looking at the references. The problem here:

[Error] D:\C-Free Standard\temp\Untitled7.cpp:372: no matching function for call to `getline (ifstream &, int &)'

is that the function getline accepts two (or three) parameters of which the second one MUSt be a string. Here is a reference: http://www.cplusplus.com/reference/string/getline/

That's no good to you if you want to write data directly into an int or a float. You're working with an object of type ifstream. Best look up that objects and see what member functions it has:

http://www.cplusplus.com/reference/iostream/ifstream/

Not much there, but look; it inherits from istream. Maybe that will be more useful:

http://www.cplusplus.com/reference/iostream/istream/

Aha, this looks promising:
operator>> Extract formatted data (public member function )

Let's have a look at that:
http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

Yes, that seems to be able to read in data and put it directly into ints and floats.

So, something like:
file1 >> year;
should do the job.

my program works fine now...thanks Moschops, really appreciate that :)

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.