#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 Initalize(Book *book, int size)
{

	for(int i=0;i>size;i++)
	{
		string t[size];
		double p[size];

	
		
		cout<<"Enter the title: ";
		cin>> t[i];
		

		cout<<"Enter the price: $";
		cin>>p[i];
		

	}



}
const int SIZE=3;

int main()
{

	int number;	
	string key;
	
	Book book;

		
	cout<< "Enter the number of books: ";
	cin>> number;
	book[SIZE]=new [number];

I have no idea. How can I make dynamically create in the main function?

Initalize(book,number);

	cout<<"Enter the keyword: ";
	cin>>key;


	system ("pause");
	return 0;

}

I want to make like the picture in the attachment.

However, I don't know how to make it. I am so stuck in class lesson.

Please help me.

Recommended Answers

All 4 Replies

The new[] operator doesn't work like other operators. To use it, you must have a pointer that you are storing the return value to.

dataType *pDataType = 0;              //declare a NULL pointer to a dataType

pDataType = new dataType[arraySize];  //create an array on the heap, store it's address in the pointer

//work with the array

delete[] pDataType;                   //destroy the array
pDataType = 0;                        //eliminate the dangling pointer created by array destruction

Thank you.

check it size value before user enter if is equal the array index then +1 this way you can use array as dynamically

check it size value before user enter if is equal the array index then +1 this way you can use array as dynamically

Huh? I hope your not saying that a loop should run from 1 to SIZE. That is totally incorrect. C++ arrays ALWAYS start at 0; therefore, the loop should run from 0 to (SIZE-1).

Example, to input a user-selected number of elements into an integer array:

#include <iostream>

using namespace std;

int main() {
  //local declarations
  int numElements = 0;
  int *intArray = 0;

  //prompt the user for array size
  cout << "How many numbers would you like to enter: ";
  cin >> numElements;
  cin.ignore();

  //create the array
  intArray = new int[numElements];

  //populate the array
  for (int i = 0; i < numElements; ++i) {
    cout << "Enter element " << i << ": ";       //prompt version 1
    //cout << "Enter element " << i+1 << ": ";   //prompt version 2
    cin >> intArray[i];
    cin.ignore();
  }

  //display the array
  cout << "Your array's contents:";
  for (int j = 0; j < numElements; ++j) {
    if (!(j % 10)) {
      cout << endl;
    }
    cout << intArray[j] << " ";
  }

  delete[] intArray;
  intArray = 0;
  return 0;
}
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.