Whats the error? Please help

Here's my 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
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();
}


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)'

How to correct them? And how are they errors? Help please!
Last edited on
Accept function takes an array of books not a single book object

To fix:
line 123 and 128 Accept(A[],i); or Accept(A,i);
How does Accept function takes an array of books and not a single book object? for one value of K it can make the user enter only one of the things of a book. Not for many books, then how does it get an array of books? If the Accept function is called once, it takes a single book object not an array of books..then how is what you are saying, true?
Last edited on
 Accept(A[],i); or Accept(A,i);

I did Accept(A[],i) and it said expression syntax error.
I did Accept(A,i) and it worked. But why did it work this way? If in the definition of Accept function the formal parameter is BOOK A[] shouldn't the actual paramater also be A[]? Or A[i]? But for the latter you said that Accept function takes in an array of BOOK not a single object but why? Isn't it clear from the definition of the Accept function that if we call it once we get one object and not an array of objects?
When you declare a function, you place the types the function can accept in the function's parameters. But when you call the function, you don't need to specify the type of the object provided the object you are passing is of the correct type.

Accept(A[],i); is wrong (That was my mistake) because you don't need to specify that 'A' is an array. You already declared 'A' as an array of books on line 115 so you just have to pass the parameter as A, the compiler knows what type A is.
Hmm thanks for that info! Ok...but you said that Accept function takes an array of objects and not a single object of array. But how? Clearly from the definition of Accept:
1
2
3
4
5
void Accept(BOOK A[], int K) //To enter the values in the array of BOOK.
{

		A[K].Enter();
}


Doesnt A[K].Enter sinply signify that at one time only one object can be entered if we call the accept function once? Then how does it take an array of objects at one time and not one single object at one time?
1
2
3
4
5
void Accept(BOOK A[], int K) //To enter the values in the array of BOOK.
{

		A[K].Enter();
}


Look at the comment beside the code and look at the bolded part in the code
Yes...but this means that to enter values in the array of BOOK not the whole array at once...so why?
Not sure I understand your question now. I never said to enter values into the array at once. I said that when you called your function in the first code, you were passing as first parameter a book object (A[i]) instead of an array of books (A). The function Accept uses the index in parameter 2 to determine what book to edit.
Okay..I got you now. Hmm..it worked. Btw, when I get the following output:
Enter the number of books you'd be entering <=20: 1
Enter details of Book number 1:
Enter Book Number: 1
Enter Book's Name: ABC
Enter Book's Author: PQR
Enter Book's price: 1234


Book Number: 1
Book Name: ABC
Author: PQR
Price :1234


There are 2 ways to search a book
1. By entering the Book Number
2. By entering the Book Name
Enter your choice: 1
Enter the book number of the book you are searching for: 1
Book 1 matches the Book Number you entered. Its details are:

Book Number: 1
Name: ABC
Author: PQR
Price: 1234
Enter the name of the book you are looking for: ABC
Book 1 matches the name you entered. Its details are:

 Book Number: 1
Name: ABC
Author: PQR
Price: 1234
Invalid Choice.


Why does it show "Invalid choice in the end? It should have been shown only if the user doesn't meet any of the required switch case statements.
Also, once I enter the choice as 1 and the program displays the required book on the basis of its book number then how does the program start the second choice of the switch case statement, i.e how does it start searching the book by name. I didn't even enter 2 as a choice and still it does that. Inf fact, if I once enter the choice as 1 it should and enter Book number it should display the required book's details and then the program should be over. But the program continues after that. Why is that? This doesn't happen when I enter "cout<<"Enter the number of books you'd be entering <=20: ";" as 2 or 3...or any nymber greater than 1. It happens only when I enter 1. Is there any reason for this error?
You may also want to read this link: http://www.gidnetwork.com/b-56.html
Thanks!
Hmm..I corrected my code. But I noticed one peculiar thing that in line 98, if I have \n\n then my output goes like:

Enter the number of books you'd be entering <=20: 3

Enter details of Book number 1:
Enter Book Number: 1
Enter Book's Name: Harry Potter and the Goblet of Fire
Enter Book's Author: J.K Rowling
Enter Book's price: 200


Enter details of Book number 2:
Enter Book Number: 2
Enter Book's Name: Scorpia
Enter Book's Author: Anthony Horowitz
Enter Book's price: 400


Enter details of Book number 3:
Enter Book Number: 3
Enter Book's Name: Ark Angel
Enter Book's Author: Anthony Horowitz
Enter Book's price: 600


Book Number: 1
Book Name: Harry Potter and the Goblet of Fire
Author: J.K Rowling
Price :200


Book Number: 2
Book Name: Scorpia
Author: Anthony Horowitz
Price :400


Book Number: 3
Book Name: Ark Angel
Author: Anthony Horowitz
Price :600


There are 2 ways to search a book
1. By entering the Book Number
2. By entering the Book Name

Enter your choice: 2

Enter the name of the book you are looking for: Scorpia

Book 2 matches the name you entered. Its details are:

 Book Number: 2 (space before Book)
Name: Scorpia
Author: Anthony Horowitz
Price: 400


That is, there is a space behind "Book" in "Book Number"

But when in line 98 if I put : <<endl<<endl; , I get the output like:
Enter the number of books you'd be entering <=20: 3

Enter details of Book number 1:
Enter Book Number: 1
Enter Book's Name: Harry Potter and the Goblet of Fire
Enter Book's Author: J.K Rowling
Enter Book's price: 200


Enter details of Book number 2:
Enter Book Number: 2
Enter Book's Name: Scorpia
Enter Book's Author: Anthony Horowitz
Enter Book's price: 400


Enter details of Book number 3:
Enter Book Number: 3
Enter Book's Name: Ark Angel
Enter Book's Author: Anthony Horowitz
Enter Book's price: 600


Book Number: 1
Book Name: Harry Potter and the Goblet of Fire
Author: J.K Rowling
Price :200


Book Number: 2
Book Name: Scorpia
Author: Anthony Horowitz
Price :400


Book Number: 3
Book Name: Ark Angel
Author: Anthony Horowitz
Price :600


There are 2 ways to search a book
1. By entering the Book Number
2. By entering the Book Name

Enter your choice: 2

Enter the name of the book you are looking for: Scorpia

Book 2 matches the name you entered. Its details are:

Book Number: 2 (No space behind Book)
Name: Scorpia
Author: Anthony Horowitz
Price: 400


That is the space behind "Book" in "Book Number" vanishes.
Why does this happen?
Line 98 has a space after the 2 newlines
cout<<"Book "<<i+1<<" matches the name you entered. Its details are:\n\n ";
Topic archived. No new replies allowed.