954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string in linked list

void insert()
{	
  	counter++;
        char id [12];
	string title;
	string author;
	int year;
	char num_id[12];
   	
	borrow *newptr,*p,*q;//declare
	p = head;
	q = NULL;
	 //counter++; 
	newptr = new borrow;//declare
			
	cout<<"\nEnter your ID: ";
	cin>>newptr->id;
	cout<<"\nTitle of the book: ";
	cin>>newptr->title;
    getline (cin, newptr->title);
	cout<<"\nAuthor: ";
	cin>>newptr->author;
	getline (cin, newptr->author);
	cout<<"\nYear: ";
	cin>>newptr->year;
	newptr->next=NULL;
	 if(head==NULL)
    {
 	 head=newptr;
 	}
  else
  {
	while(p!= NULL && strcmp(newptr->id,p->id)>0)
	//while(p!= NULL && (newptr->id > p->id && newptr->id > p->id))
	{
		q=p;
		p=p->next;
	}
	if (q==NULL)
	{
		newptr->next=head;
		head=newptr;
	}
	else
	{
		newptr->next=p;
		q->next=newptr;
	}
  }

}
		
void display()
{	int n=0,newptr;
	pinjambuku *cur;
	cur = head;

	while (cur != NULL)
	{ 
	cout <<cur->id<<endl;
	cout <<cur->title<<endl;
	cout <<cur->author<<endl;	
	cout <<cur->year<<endl;
	cur=cur->next;
	n++;
	}
	cout<<" Total of ID  :"<<n<<endl;
}


*** when i entered harry potter in the title, it appear potter and the same goes to the author which when i entered jack sparrow, it will print sparrow, anyone can help me?

MugANDGlass
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Just use getline() when you read strings from the user. Remove cin>>newptr->title and cin>>newptr->author from your code.

mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
 

Beware of mixing calls to >> and getline() in the same program. The default termination character for getline() is the newline char. getline() will stop putting additional information into the target string whenever if finds the terminating char. getline() will remove the terminating char from the input stream. White space characters (newline, tab, etc) are the terminating characters for >>. >> does not remove the terminating char from input stream. >> and getline() fequently use the same input stream, for example, both use cin in your program.

In your program >> is called before getline() is called and is terminated by newline. getline() is using the default newline char as the terminating char. Therefore, the first thing getline sees is the newline char left in the input stream by the preceding call to >> and it doesn't put the expected information into the expected variarble. It doesn't matter whether >> is called immediately before getline() or not in the programming code.

Options to deal with this are:
1) Don't mix >> and getline() in the same program.
2) Attempt to clear the input stream before every call to getline() if you do mix >> and getline().

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: