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

class Book
{

	private:

		string title;

		double price;

	public:

		Book();

		void setTitle(string s);

		void setPrice(double p);

		string getTitle() const
		
		{return title;}


		double getPrice() const
		
		{return price;}

		bool hasKeyword(string key);


	
};

Book::Book()
{
	title="None";
	price=0;
}

void Book :: setTitle(string s)
{
	title=s;

}

void Book::setPrice(double p)
{
	if(p>0)
	{
		price=p;
	
	}

	else
	{
		price=0;
	}

}



void Initialize(Book *book, int size)
{

This for loop doesn't work. Can you tell me why? or fix it? thank you.

for(int i=0;i>size;i++)
	{
		string T;
		double P;

		cout<<"Enter the title: ";
		cin>>T;
		

		cout<<"Enter the price: $";
		cin>>P;
		
		book[i].setTitle(T);
		book[i].setPrice(P);
		

	}



}
const int SIZE=3;

int main()
{

	int number;	
	string key;

		
	cout<< "Enter the number of books: ";
	cin>> number;

	
	Book *book=0;
	book=new Book[number];
	


	Initialize(book,number);
cout<<"Enter the keyword: ";
	cin>>key;

	
	
	
	
	

	delete[] book;
	book=0;


	system ("pause");
	return 0;

}

can you fix initialize function?

Recommended Answers

All 4 Replies

When you enter the number of books, the \n you typed is still in the input buffer. This is read as the first input in Initialize(). Look at the cin.ignore() method

cin.ignore() doesn't work.

I think in lines #81 and #82 you have to use the -> dereferrence operator as opposed to the dot operator.

I found my code problem. anyway thank you for reply.

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.