new operator information

In the code below when I try to allocate memory using new it shows error cannot convert book * to book[].I think I am doing something wrong.

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
//Using namespace std
class book
{
private:
char *author;
char title[50];
int price;
char publisher[50];
int stock_position;
public:
book(int x)//constructor defined
{
author=new char[x];
}
void getdata();
void display();
};//class ends
void book :: getdata()
{
cout<<"Enter title of book";
cin>>title;
cout<<"Enter author of book";
cin>>author;
cout<<"Enter price of book";
cin>>price;
cout<<"Enter publisher of book";
cin>>publisher;
cout<<"Enter number of copies";
cin>>stock_position;
}
void book :: display()
{
cout<<setw(20)<<title<<setw(20)<<author<<setw(20)<<Publisher<<setw(20)<<price
<<setw(10)<<stock position;
}
main()
{
int size;
cout<<"How many character you want in author name\n";
cin>>size;
book ob[50]=new book(size);
char bname[50];
int choice,nbook;
while(1)
{
cout<<"Menu:1.Input Data 2.Display 3.search book 4.Purchase book 5.Exit";
Cout<<"\n Enter your choice";
cin>> choice;
switch(choice)
{
case 1:
Cout<<"How many books data you want to enter";
Cin>>nbook;
For(i=0;i<nbook;i++)
ob[i].getdata();
break;
case 2:
if (nbook<=0)
cout<<"No data available";
else
{
cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"<<setw(20)<<"prices"<<setw(10)<<"stock position";
for(i=0;i<nbook;i++)
{
ob[i].putdata();
}
}
break;
case 3:
if (nbook<=0)
cout<<"No data available";
else
{
cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"<<setw(20)<<"prices"<<setw(10)<<"stock position";
for(i=0;i<nbook;i++)
{
cout<<"Enter book name you want to search";
cin>>bname;
if (strcmp(bname,ob[i].title)==0)
{
ob[i].putdata();
}
}
}
break;
case 4:
if (nbook<=0)
cout<<"No data available";
else
{
for(i=0;i<nbook;i++)
{
cout<<"Enter book name you want to purchase";
cin>>bname;
if (strcmp(bname,ob[i].title)==0)
{
cout<<"book details are given below";
cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"<<setw(20)<<"prices"<<setw(10)<<"stock position";
ob[i].putdata();
cout<<"How many copies you want to puchase";
cin>>copies;
If (ob[i].stock_position>=copies)
{
cout<<"Requested copies are available\n";
cout<<"Total cost is:\t"<<copies*ob[i].price;
}
else
{
cout<<"Sorry!!!!!Requested copies arenot available\n";
}
}
}
break;
case 5:
exit(0);
}//while ends
}//main ends
}
book ob[50]=new book(size);
It should be
book* ob = new book[ size ];

Apart from that, i tried to compile ur code and got more errors,

Are you still using turbo c ?
the book constructor is like book(int) so how can I use square brackets like book[] ?.yes there are more errors also.But now I am trying to fix this one.I use TC,Dev Cpp on windows and on fedora Eclipse, any other good compiler is there.
Last edited on
Eclipse has a Windows client, why not just use that? Otherwise wxDev-Cpp is another option, although it is buggy and often completely breaks for no apparent reason. Literally anything but Turbo-C would be better then what you have now. You're not learning either C or C++ but instead you're just using some bastardized in-between version that doesn't subscribe to either standard. Sticking with this tool is not going to help you in the future.
Topic archived. No new replies allowed.