What to do?

Hi. This is one of the programs that I have to do according to my assignment:
Define a class BOOK with the following members:
Private Members:
Bno- Of type int
Bname- Of type char array size of 20
Author- Of type char array size 20
Price- Of type float
Public Members:
void Enter()- Member function to enter the values of data members
void Display()- Member function to display the values of data members
int RBno()- Member function that returns the value of Bno
char*RBname()- Member function that returns the value of Bname

Write a menu driven prgram with separate functions for each of the follwing for an array of BOOK type:
-To enter values in the array of BOOK
-To display the details from an array of BOOK passed through parameter
-To search for a BOOK from the array upon its Bno and display its details
-To search for a BOOK from the array upon its Bname and display its details


For this I wrote the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class BOOK
{
  private:
	  int Bno;
		  char Bname[20];
		  char Author[20];
		  float Price;
  public:
	      void Enter()
		  {
			  cout<<"Enter Book Number: ";
			  cin>>Bno;
			  cout<<"Enter Book's Name: ";
			  gets(Bname);
			  cout<<"Enter Book's Author: ";
			  gets(Author);
			  cout<<"Enter Book's price: ";
			  cin>>Price;
		  }


		  int RBno()
		  {return Bno;}


		  char* RBname()
		  {return Bname;}

};



void BOOK::Display(BOOK A[], int N)
		  {

			  for(int i=0; i<N; i++)
			  {
			   cout<<"The details of Book "<<i+1<<" are:"<<endl<<endl;
			   cout<<"Book Number: "<<A[i].Bno<<endl;
			   cout<<"Name: "<<A[i].Bname<<endl;
			   cout<<"Author: "<<A[i].Author<<endl;
			   cout<<"Price: "<<A[i].Price<<endl;
			   cout<<endl;
			  }


		  }

void main()
{
	clrscr();
	int n, choice, x, found1=0, found2=0; char Input[20];
    BOOK A[20];
	do
	{
	  cout<<"Enter the number of books you'd be entering <=20: ";
	  cin>>n;
	}while(n>20);
    for(int i=0; i<n ; i++)
	{
		A[i].Enter();
	cout<<endl<<endl;
	}
	BOOK::Display(A,n);
	cout<<"There are 2 ways to search a book"<<endl;
	cout<<"1. By entering the Book Number"<<endl;
	cout<<"2. By entering the Book Name"<<endl;;
	cout<<"Enter your choice: ";
	cin>>choice;
	switch(choice)
	{

	case 1:
		{

		   cout<<"Enter the book number of the book you are searching for: ";
	       cin>>x;
		  for(i=0; i<n; i++)
		  {
		   if(A[i].RBno()==x)
		   {
			  cout<<"Book "<<i+1<<" matches the Book Number you entered. Its details are:<<endl<<endl;";
			  cout<<"Book Number: "<<A[i].Bno<<endl;
			  cout<<"Name: "<<A[i].Bname<<endl;
			  cout<<"Author: "<<A[i].Author<<endl;
			  cout<<"Price: "<<A[i].Price<<endl;
			  found1++;
		   }
		  if(!found1)
			cout<<"No book matches the Book Number you entered.";
		  }
		  break;
		}
	case 2:
		{
			cout<<"Enter the name of the book you are looking for: ";
			gets(Input);
			for(i=0; i<n; i++)
			{
				if(!(strcmpi(A[i].RBname(),Input)))
				{
					cout<<"Book "<<i+1<<" matches the name you entered. Its details are:\n\n ";
		    cout<<"Book Number: "<<A[i].Bno<<"\n";
				cout<<"Name: "<<A[i].Bname<<"\n";
				cout<<"Author: "<<A[i].Author<<"\n";
				    cout<<"Price: "<<A[i].Price<<"\n";
		    found2++;
				}
			}
				if(!found2)
					cout<<"Sorry. No book matches the name you entered.";
				break;
		}


	}
	getch();
}


But get the following errors:

1)Line 39: 'Book::Display(Book*,int)' is not a member of 'BOOK'
2)Line 69: Undefined symbol 'Display'
3)Line 88: 'BOOK::Bno' is not accessible. 
4)Line 89: 'BOOK::Bname' is not accessible.
5)Line 90: 'BOOK::Author' is not accessible.
6)Line 91: 'BOOK::Price' is not accessible.
7)Line 108: 'BOOK::Bno' is not accessible.
8)Line 109: 'BOOK::Bname' is not accessible.
9)Line 110': BOOK::Author' is not accessible.
10)Line 111: 'BOOK::Price' is not accessible.


I know that errors 3 to 10 are because those members are in private. But then I have to use them, so how can I as long as they are in private?

I didn't understand errors 1 and 2. Please reply according to the version of C++ I have used. Thanks.
> 'Book::Display(Book*,int)' is not a member of 'BOOK'
In your class definition you ought to declare all the member functions that it has.
So
1
2
3
4
5
6
class BOOK
{
//...
  public:
		  static void Display(BOOK A[], int N); //declaration
};
(static because you don't need an instance to call it, it is a function of the class)


> But then I have to use them, so how can I as long as they are in private?
You don't have to use them. You need to perform operations, that you assumed would affect them.
Use the interface of your class.
By instance, according to your assignment
> void Display()- Member function to display the values of data members
So lines 88-91 would be A[i].Display(); instead
(don't confuse with the other display function, you have not coded this one yet)


> Please reply according to the version of C++ I have used.
¿which one is that?
You are violating the c++{98,11} standards
_ main must return int
_ the header are `iostream', `cstdio', `cstring'
_ `cout' is in the `std' namespace
_ `gets()' is deprecated

Try to follow the standard, you'll benefit.
I can't follow the new C++ version as my school is still running the older one and I have to work according to that. So, I updated my program as follows to minimize the errors from 10 to 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class BOOK
{
  private:
	      int Bno;
		  char Bname[20];
		  char Author[20];
		  float Price;
  public:
	      void Enter()
		  {
			  cout<<"Enter Book Number: ";
			  cin>>Bno;
			  cout<<"Enter Book's Name: ";
			  gets(Bname);
			  cout<<"Enter Book's Author: ";
			  gets(Author);
			  cout<<"Enter Book's price: ";
			  cin>>Price;
		  }


		  void Display()
		  {
			  cout<<"Book Number: "<<Bno<<endl;
			  cout<<"Book Name: "<<Bname<<endl;
			  cout<<"Author: "<<Author<<endl;
			  cout<<"Price :"<<Price<<endl;
		  }

	   int retbno()
		{return Bno;}

	   char* retbname()
	   {return Bname;}

	   char* retauthor()
	   {return Author;}

	   float retprice()
	   {return Price;}


};




void Accept(BOOK A[], int K) //To enter the values in the array of BOOK.
{

		A[K].Enter();
}

void Output(BOOK A[], int K) //To display the details from an array of BOOK passed through parameter.
{
	   A[K].Display();
}


void SearchBookByBno(BOOK A[], int N) //To search for a BOOK from the array upon its Bno and display its details.
{
   int n, found1=0;
		   cout<<"Enter the book number of the book you are searching for: ";
	       cin>>n;
		  for(int i=0; i<N; i++)
		  {
		   if(A[i].retbno()==n)
		   {
			  cout<<"Book "<<i+1<<" matches the Book Number you entered. Its details are:<<endl<<endl;";
			  cout<<"Book Number: "<<A[i].retbno()<<endl;
			  cout<<"Name: "<<A[i].retbname()<<endl;
			  cout<<"Author: "<<A[i].retauthor()<<endl;
			  cout<<"Price: "<<A[i].retprice()<<endl;
			  found1++;
		   }
		  }
		  if(!found1)
			cout<<"Sorry. No book matches the Book Number you entered.";
}

void SearchBookByName(BOOK A[], int N)
{

	char Input[20];
	int found2=0;

		cout<<"Enter the name of the book you are looking for: ";
		gets(Input);
		for(int i=0; i<N; i++)
		{
			if(!(strcmpi(A[i].retbname(),Input)))
			{
			  cout<<"Book "<<i+1<<" matches the name you entered. Its details are:\n\n ";
		      cout<<"Book Number: "<<A[i].retbno()<<"\n";
			  cout<<"Name: "<<A[i].retbname()<<"\n";
			  cout<<"Author: "<<A[i].retauthor()<<"\n";
			  cout<<"Price: "<<A[i].retprice()<<"\n";
		      found2++;
			}
		}
				if(!found2)
					cout<<"Sorry. No book matches the name you entered.";
}


void main()
{
	clrscr();
	int n, choice;
    BOOK A[20];
	do
	{
	  cout<<"Enter the number of books you'd be entering <=20: ";
	  cin>>n;
	}while(n>20);
    for(int i=0; i<n ; i++)
	{
		Accept(A[i],i);
	cout<<endl<<endl;
	}
    for(i=0; i<n; i++)
	{
	    Output(A[i],i);
	 cout<<endl<<endl;
	}
	
	cout<<"There are 2 ways to search a book"<<endl;
	cout<<"1. By entering the Book Number"<<endl;
	cout<<"2. By entering the Book Name"<<endl;;
	cout<<"Enter your choice: ";
	cin>>choice;
	switch(choice)
	{

		case 1:
			SearchBookByBno(A,n);
		case 2:
			SearchBookByName(A,n);
        default:
			cout<<"Invaild Choice"<<endl;
	}

	getch();
}


But now the errors are:
1)Line 123: Cannot convert 'BOOK' to 'BOOK *'
2)Line 123: Type mismatch in parameter 'A' in call to 'Accept(BOOK *, int)'
3)Line 128: Cannot convert 'BOOK' to 'BOOK *'
4)Line 128: Type mismatch in parameter 'A' in call to 'Accept(BOOK *, int)'

Tried a lot but I am not able to remove these errors..
void Accept(BOOK A[], int K)

I think, BOOK A[] is a pointer to an element in an array, you may want clarification on this.

But any way you're trying to pass an object of type BOOK to type BOOK[], which of course wouldn't fly. Judging by the way you're passing the argument to the function, you may want this instead:

1
2
3
4
void Accept(BOOK& A)
{
    A.Enter();
}


Note that A is a reference.
I think that the idea is
1
2
3
4
5
6
7
8
void Output(BOOK A[], int size) //To display the details from the whole array
{
   for(int K=0; K<size; ++K)
      A[K].Display();
}

//126-130
Output( A, n );


> I can't follow the new C++ version as my school is still running the older one
> and I have to work according to that.
1998 is not new.
Try to talk to your teacher.
Last edited on
Can you please write the updated code? I am unable to do so by what you said.. :/
Maybe you are doing something wrong, it would be good that you show your current code.
Topic archived. No new replies allowed.