Structure related query

In this code I am getting structure required on left side of . or .* error on this line

ob[i].getdata();

please let me know am I doing anything 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];
char *a=new char[10];
}
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; int i;
cout<<"How many character you want in author name\n";

cin>>size;
book *ob[5];
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
}



Last edited on
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

book *ob[5];

ob is an array of pointers to book, not an array of books.


ob[50]=new book(size);

ob has valid indexes 0, 1, 2, 3, and 4, so element 50 is way outside the valid range - you're corrupting memory here.
Last edited on
Not getting it.Can you simplify it ? If i change book*ob[50]; then ?
I said three different things in my post, could you specify which one you're referring to? Please be specific about what is confusing you.
code tags is ok.
If i use
book *ob[50]
then it will work ?
How to get working the code I want to remove the error
structure required on left side of . or .*
mayur21 wrote:
If i use
book *ob[50]
then it will work ?
That will fix the memory corruption, but not your error - I will get to that in a moment.
mayur21 wrote:
How to get working the code I want to remove the error
structure required on left side of . or .*
Use the -> operator instead, just as you would use for pointers not in an array.

EDIT: Fixed mismatched tags
Last edited on
Thanks LB.Now I have solved my problem and the code is working.Thanks for all your support.
Topic archived. No new replies allowed.