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

	while (!file1.eof())
	{
		newptr = new book;  //
		
		file1.getline(id,12);
		getline(file1,title);
		getline(file1,author);
		getline(file1,year);
		file1.getline(price);
		
               //copy values from the file to the newptr		
		newptr->id.assign (id) ;
		newptr->tajuk.assign (title) ;
		newptr->penulis.assign(author) ;
		newptr->tahun.assign (year) ;
		newptr->harga.assign(price) ;
		
		
		
		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();

	return ;

}

void newdisplay() 
{
	system("cls");
	char id [12];
	string title;
	string author;
	int year;
	float price;
	ifstream file1;

	file1.open("books.txt");
cout<<"________________________________________________________________________________________________________________"<<endl;
cout<<"\n\tID:\tTITLE:\tAUTHOR:\tYEAR:\tPRICE:<<endl;
cout<<"________________________________________________________________________________________________________________"<<endl;
	if (file1.fail())
	{
		cout << "Error opening file.\n";
		exit(1);
	}

	while (!file1.eof())
	{
		getline(file1,id);
		getline(file1,title);
		getline(file1,author);
		getline(file1,year);
		getline(file1,price);
		cout <<"\n"<< id<<"\t"<<title<<"\t"<<author<<"\t"<<year<<"\t"<<price<<"\t"<<endl;
	}

	file1.close();

	return ;
}

when i compile;
[Error] D:\My Documents\Downloads\file.cpp:370: no matching function for call to `ifstream::getline (_IO_istream_withassign &, int &)'
[Error] D:\C-FREE~1\mingw32\Include\G__~1\iostream.h:129: candidates are: class istream & istream::getline(char *, int, char = '\n')
[Error] D:\My Documents\Downloads\file.cpp:371: no matching function for call to `ifstream::getline (float &)'
[Error] D:\My Documents\Downloads\file.cpp:374: request for member `assign' in `newptr->book::id', which is of non-aggregate type `char[12]'
[Error] D:\My Documents\Downloads\file.cpp:377: request for member `assign' in `newptr->book::year', which is of non-aggregate type `int'
[Error] D:\My Documents\Downloads\file.cpp:389: request for member `compare' in `p->book::id', which is of non-aggregate type `char[12]'
[Error] D:\My Documents\Downloads\file.cpp:443: no matching function for call to `getline (ifstream &, char[12])'

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Lerner
0

Good of you to post only relevant code and relevant error messages, although in the future it would help if you correlate the line(s) referred to in the error message to the code posted, because the line numbers will likely be different.

In this case I suspect the problem is that year and price are declared as type int and float but getline() can only be used with C style strings or STL string objects. You should either use >> to get the input for those variables or input them into a holding variable of type string and then convert them to numerical values if you want with an istringstream or some other (less desirable) convention.

Note: If you elect to use >> to input the variables you should know there is a classic run time bug waiting to happen because of the difference between how >> and getline() handle the terminating char. This bug only happens if you combine >> and getline() in the same program, call >> sometime in the program before you call getline() and don't clear the input buffer before callig getline(). Once you've encountered it and know what it's about you can avoid it if you prefer to mix the input methods.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.