This is some crazy error and is giving me a lot of trouble.
#include <iostream>
using namespace std;
class Book {
private:
int bookid;
char bookname[50];
char authorname[50];
float cost;
public:
void getinfo(void) {
for (int i = 0; i < 5; i++) {
cout << "Enter Book ID" <<endl;
cin >> bookid;
cout << "Enter Book Name" << endl;
cin >> bookname;
cout << "Enter Author Name" << endl;
cin >> authorname;
cout << "Enter Cost" << endl;
cin >> cost;
}
}
void displayinfo(void);
};
int main()
{
Book bk[5];
for (int i = 0; i < 5; i++) {
bk[i].getinfo();
}
void Book::displayinfo() {
for(int i = 0; i < 5; i++) {
cout << bk[i].bookid;
cout << bk[i].bookname;
cout << bk[i].authorname;
cout << bk[i].cost;
}
}
return 0;
}
The error, as noted in the title is expected declaration before '}' token at the line void Book::displayinfo() in main
Also this error is coming expected '}' at end of input
Move the function definition void Book::displayinfo(){}
out of the main()
.
Along with this, i have some more suggestion for you. Update your class definition like this
class Book{
private:
int bookid;
string bookname; // char bookname[50]; because it can accept book name length more than 50 character.
string authorname; // char authorname[50]; because it can accept authorname length more than 50 character.
float cost;
public:
void getinfo(void){
for(int i =0; i < 5; i++){
cout << "Enter Book ID" <<endl;
cin >> bookid;
cout << "Enter Book Name" << endl;
getline(cin,bookname); // Because book name can have spaces.
cout << "Enter Author Name" << endl;
getline(cin,authorname); // Because author name can have spaces too.
cout << "Enter Cost" << endl;
cin >> cost;
}
}
void displayinfo(void);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With