7 Replies - 1090 Views - Last Post: 04 April 2012 - 06:13 PM Rate Topic: ***-- 2 Votes

#1 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Skipping Book Name while Printing..

Posted 04 April 2012 - 09:29 AM

Here is my code. There are 4 string type and 1 int type data which i have to enter in the program.

	string Name;
	string Author;
	string Genre;
	int Price;
	string Publisher;



when i enter all the details using
cin>> 
its working when i call the print function but when i enter the details using
getline(cin, xxxx) 
that code and that
 cin.ignore 
then while printing it is skipping the "Book Name" while printing.. :'( why? can anyone fix it?

void main()
{
		cout<<" Enter Book Name: ";
		getline(cin, Name);
		cin.ignore(1000, '\n');

		cout<<" Enter Author Name: ";
		getline(cin, Author);
	
		cout<<" Enter Book Genre: ";
		getline(cin, Genre);

		cout<<" Enter Book Price: ";
		cin>>Price;
		cin.ignore(1000, '\n');

		cout<<" Enter Publisher Name: ";
		getline(cin, Publisher);
}



..

Thanks in adv, here is the screen shot !! must check the output !!!!!

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Skipping Book Name while Printing..

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,689
  • Joined: 25-December 09

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 10:11 AM

Quote

then while printing it is skipping the "Book Name" while printing.. :'( why? can anyone fix it?


You need to show the function that is doing the printing. A complete program would be best.

Also main must be defined as returning an int and you should return an int from that function: int main(). And you should not use global variables.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 10:16 AM

View Postjimblumberg, on 04 April 2012 - 10:11 AM, said:

Quote

then while printing it is skipping the "Book Name" while printing.. :'( why? can anyone fix it?


You need to show the function that is doing the printing. A complete program would be best.

Also main must be defined as returning an int and you should return an int from that function: int main(). And you should not use global variables.

Jim





Thats doubly link list print function!!! I'm making a library system

void BookDoubly::Prints()
{
	Node *n=head;
	while(n->next!=0)
	{
		cout<<"***** Book Detail *****"<<endl;
		cout<<"\n Name: "<<n->Name<<endl;
		cout<<"	Author: "<<n->Author<<endl;
		cout<<"	Genre: "<<n->Genre<<endl;
		cout<<"	Price: "<<n->Price<<endl;
		cout<<"	Publisher: "<<n->Publisher<<endl;
		n=n->next;
	}
	cout<<"\n Name: "<<n->Name<<endl;
	cout<<"	Author: "<<n->Author<<endl;
	cout<<"	Genre: "<<n->Genre<<endl;
	cout<<"	Price: "<<n->Price<<endl;
	cout<<"	Publisher: "<<n->Publisher<<endl;	
}




Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,689
  • Joined: 25-December 09

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 10:26 AM

Post the smallest possible complete program that illustrates your problem.

What is the purpose of your while loop? Do you have the same problem when there are more that one entry in your list?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 10:42 AM

View Postjimblumberg, on 04 April 2012 - 10:26 AM, said:

Post the smallest possible complete program that illustrates your problem.

What is the purpose of your while loop? Do you have the same problem when there are more that one entry in your list?

Jim



okay here is my full code!!!!!!..


#include<iostream>
using namespace std;
#include<string>

class Node
{
public:
	string Name;
	string Author;
	//int Id; //********** unique honi chaye **********
	string Genre;
	int Price;
	string Publisher;
	//int Copies; //********** total + available **********
	Node *next;
	Node *prev;
	
	//********** Parameterize constructer **********
	Node(string n, string a, int i, string g, int pri, string pub, int cop){
		Name=n;
		Author=a;
		//Id=i;
		Genre=g;
		Price=pri;
		Publisher=pub;
		//Copies=cop;
	}
	
	//********** Default Constructer **********
	Node()
	{
		next=0;
		prev=0;
		cout<<" Enter Book Name: ";
		getline(cin, Name);
		cin.ignore(1000, '\n');
		cout<<" Enter Author Name: ";
		getline(cin, Author);
		//cin>>Id;
		cout<<" Enter Book Genre: ";
		getline(cin, Genre);
		cout<<" Enter Book Price: ";
		cin>>Price;
		cout<<" Enter Publisher Name: ";
		getline(cin, Publisher);
		cin.ignore(1000, '\n');
		/*cout<<" Enter The Number of Copies: \n";
		cin>>Copies;*/
	}
};





#include"NodeB.h"

class BookDoubly
{
private:
	Node* head; 
	Node* tail;
public:
	BookDoubly()
	{
		head=tail=0;
	}
	//~BookDoubly();
	bool IsEmpty()
	{
		if(head==0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	void addBookToHead(); 
	void addBookToTail();
	void deleteBookFromHead();
	void deleteBookFromTail();
	void Delete();
	void SearchbyName();
	void SearchbyPublisher();
	void SearchbyAuthor();
	//void SearchbyID();
	void Prints();
	//void AddCopy();
};

// ********** Add Book To Head ********** 

void BookDoubly::addBookToHead()
{
	Node *np= new Node();
	if (IsEmpty())
	{
		head = tail = np;
	}
	else
	{
		np->next=head;
		head->prev=np;
		head=np;
	}
}

// ********** Add Book To Tail ********** 

void BookDoubly::addBookToTail()
{
	Node *np= new Node();
	if (IsEmpty())
	{
		head = tail = np;
	}
	else
	{
		tail->next=np;
		np->prev=tail;
		tail=np;
	}
}


// ********** Delete Book From Head ********** 
void BookDoubly::deleteBookFromHead()
{
	if (!IsEmpty())
	{
		if (head==tail)
		{
			delete head;
			head = tail = 0;
		}
		else
		{
			head=head->next;
			delete (head->prev);
			head->prev=0;
		}
	}
}

// ********** Delete Book From Tail ********** 

void BookDoubly::deleteBookFromTail()
{
	if (!IsEmpty())
	{
		if (head==tail)
		{
			delete head;
			head = tail = 0;
		}
		else
		{
			tail=tail->prev;
			delete (tail->next);
			tail->next=0;
		}
	}
}

// ********** Delete ********** 

void BookDoubly::Delete()
{
	string del;
	cout<<"Enter Book Name Which You Want To Delete "<<endl;
	getline(cin, del);
	cin.ignore(1000, '\n');
	if(!IsEmpty())
	{
		if(head->Name==del)
		{
			deleteBookFromHead();
		}
		else if(tail->Name==del)
		{
			deleteBookFromTail();
		}
		else
		{
			Node *np=head;
			while(np->Name!=del && np!=0)
			{
				np=np->next;
			}
			if(np!=0)
			{
				Node *p = np->prev;
				Node *q = np->next;
				p->next = q;
				q->prev = p;
				delete np;
				cout<<"The Element You Entered is Successfully Deleted !!"<<endl;
			}
			else
			{
				cout<<"Not Found !"<<endl;
			}
		}
	}
}


// ********** Search Book by Name ********** 
void BookDoubly::SearchbyName()
{
	string nm;
	cout<<"    "<<endl;
	cout<<" Enter Book Name Which you Want to Search: ";
	getline(cin, nm);
	cin.ignore(1000, '\n');
	if(head!=0)
	{
		Node*n=head;
		while(n!=0 && n->Name!=nm)
			n=n->next;
		if(n!=0)
		{
			cout<<"	The Book \""<<n->Name<<"\" is Availlable !!!"<<endl;
		}
		else
		{
			cout<<"	Sorry! Desire Book not Found !!!"<<endl;
		}
	}
}


// ********** Search Book by Publisher ********** 
void BookDoubly::SearchbyPublisher()
{
	string nm;
	cout<<"     "<<endl;
	cout<<" Enter Publisher Name To Search the Book: ";
	getline(cin, nm);
	cin.ignore(1000, '\n');
	if(head!=0)
	{
		Node*n=head;
		while(n!=0 && n->Publisher!=nm)
			n=n->next;
		if(n!=0)
		{
			cout<<"	Book by Publisher \""<<n->Publisher<<"\" is Available !!!"<<endl;
		}
		else
		{
			cout<<" Sorry! Desire Book not Found !!!"<<endl;
		}
	}
}


// ********** Search Book by Author **********  
void BookDoubly::SearchbyAuthor()
{
	string nm;
	cout<<"     "<<endl;
	cout<<" Enter Author Name To Search the Desire Book: ";
	getline(cin, nm);
	cin.ignore(1000, '\n');
	if(head!=0)
	{
		Node*n=head;
		while(n!=0 && n->Author!=nm)
			n=n->next;
		if(n!=0)
		{
			cout<<"	Book by Author \""<<n->Author<<"\" is Available !!!"<<endl;
		}
		else
		{
			cout<<"	Sorry! Desire Book not Found !!!"<<endl;
		}
	}
}


// ********** Print All The Things ********** 
void BookDoubly::Prints()
{
	Node *n=head;
	while(n->next!=0)
	{
		cout<<"***** Book Detail *****"<<endl;
		cout<<"\n Name: "<<n->Name<<endl;
		cout<<"	Author: "<<n->Author<<endl;
		cout<<"	Genre: "<<n->Genre<<endl;
		cout<<"	Price: "<<n->Price<<endl;
		cout<<"	Publisher: "<<n->Publisher<<endl;
		n=n->next;
	}
	cout<<"\n Name: "<<n->Name<<endl;
	cout<<"	Author: "<<n->Author<<endl;
	cout<<"	Genre: "<<n->Genre<<endl;
	cout<<"	Price: "<<n->Price<<endl;
	cout<<"	Publisher: "<<n->Publisher<<endl;	
}






#include"BookDoubly.h"

void main()
{
	BookDoubly bd;
	char a;
	do
	{
		cout<<"       "<<endl;
		cout<<" Press 1 For Add Book "<<endl;
		cout<<" Press 2 For Delete Book "<<endl;
		cout<<" Press 3 For Print The Details "<<endl;
		//cout<<" Press 4 For Add Copy of Book "<<endl;
		cout<<" Press 5 For Search "<<endl;
		cout<<" Press 6 For Exxit !! "<<endl;
		cout<<"       "<<endl;
		cin>>a;
		switch(a)
		{
		case'1':
			bd.addBookToHead();
			break;
		/*case '2':
			bd.Delete();*/
			break;
		case '3':
			bd.Prints();
			break;
		/*case '4':
		
		break;*/
		case '5':
			char y;
			do
			{
				cout<<"       "<<endl;
				cout<<"Press 1 For Search Book by Name "<<endl;
				cout<<"Press 2 For Search Book by Author "<<endl;
				cout<<"Press 3 For Search Book by Publisher "<<endl;
				cout<<"Press 4 For Back To Main Menu "<<endl;
				cout<<"       "<<endl;
				cin>>y;
				switch(y)
				{
				case '1':
					bd.SearchbyName();
					break;
				case '2':
					bd.SearchbyAuthor();
					break;
				case '3':
					bd.SearchbyPublisher();
					break;
				} 
			} while (!(y=='4'));
		}
	} while (!(a=='6'));
	cout<<"       "<<endl;
	cout<<" Bubye - See You Next Time "<<endl;
	system("pause");
}


This post has been edited by joannesablad: 04 April 2012 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 10:50 AM

Code looks fine from here. I'd strongly recommend not mixing up your node with your data. Make a book type. Have methods for it.

Your code worked for me in this test:
#include <iostream>

using namespace std;

struct Book {
	string Name;
	string Author;
	string Genre;
	int Price;
	string Publisher;
};

struct Node {
	Book book;
	Node *next;
	Node(const Book &b, Node *n = NULL) : book(B)/>, next(n) { }
};


void print(const Book &);
void getBookFromUser(Book &);


int main() {
	Book b;
	
	getBookFromUser(B)/>;
	print(B)/>;
	
	return 0;
}; 


void print(const Book &book) {
	cout << "Name: " << book.Name << endl;
	cout << "Author: "<< book.Author << endl;
	cout << "Genre: "<< book.Genre << endl;
	cout << "Price: "<< book.Price << endl;
	cout << "Publisher: "<< book.Publisher << endl;
}


void getBookFromUser(Book &book) {
	cout<<" Enter Book Name: ";
	getline(cin, book.Name);
	// cin.ignore(1000, '\n');

	cout<<" Enter Author Name: ";
	getline(cin, book.Author);

	cout<<" Enter Book Genre: ";
	getline(cin, book.Genre);

	cout<<" Enter Book Price: ";
	cin >> book.Price;
	cin.ignore(1000, '\n');

	cout<<" Enter Publisher Name: ";
	getline(cin, book.Publisher);
}


Was This Post Helpful? 0
  • +
  • -

#7 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 12:10 PM

View Postbaavgai, on 04 April 2012 - 10:50 AM, said:

Code looks fine from here. I'd strongly recommend not mixing up your node with your data. Make a book type. Have methods for it.

Your code worked for me in this test:
#include <iostream>

using namespace std;

struct Book {
	string Name;
	string Author;
	string Genre;
	int Price;
	string Publisher;
};

struct Node {
	Book book;
	Node *next;
	Node(const Book &b, Node *n = NULL) : book(B)/>, next(n) { }
};


void print(const Book &);
void getBookFromUser(Book &);


int main() {
	Book b;
	
	getBookFromUser(B)/>;
	print(B)/>;
	
	return 0;
}; 


void print(const Book &book) {
	cout << "Name: " << book.Name << endl;
	cout << "Author: "<< book.Author << endl;
	cout << "Genre: "<< book.Genre << endl;
	cout << "Price: "<< book.Price << endl;
	cout << "Publisher: "<< book.Publisher << endl;
}


void getBookFromUser(Book &book) {
	cout<<" Enter Book Name: ";
	getline(cin, book.Name);
	// cin.ignore(1000, '\n');

	cout<<" Enter Author Name: ";
	getline(cin, book.Author);

	cout<<" Enter Book Genre: ";
	getline(cin, book.Genre);

	cout<<" Enter Book Price: ";
	cin >> book.Price;
	cin.ignore(1000, '\n');

	cout<<" Enter Publisher Name: ";
	getline(cin, book.Publisher);
}




well i have to made that code using node and doublylinklist.. not from struct..
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Skipping Book Name while Printing..

Posted 04 April 2012 - 06:13 PM

In main you read from cin this call leaves a newline in the stream. The getline function reads the newline and sets Name to an empty string.

17	        cin>>a;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1