In this program, you'll learn to declare structure "book" having data member as book_name, book_id, book_price, author. Accept this data for 3 books and display it.
#include<iostream>
using namespace std;
struct book
{
char book_name[20];
int bookid;
float book_price;
char author[15];
};
int main()
{
struct book b[3];
int i;
for(i=0; i<3; i++)
{
cout<<"\nEnter details of book #"<< i+1;
cout<<"\nEnter book id: ";
cin>> b[i].bookid;
cout<<"Enter book name: ";
cin>> b[i].book_name;
cout<<"Enter book author: ";
cin>> b[i].author;
cout<<"Enter book price: ";
cin>> b[i].book_price;
}
for(i=0; i<3; i++)
{
cout<<"\n\n*** Book "<< i+1 << " ***";
cout<<"\nBook Id: " << b[i].bookid;
cout<<"\nBook Name: " << b[i].book_name;
cout<<"\nBook Author: " << b[i].author;
cout<<"\nBook price: " << b[i].book_price;
}
return 0;
}
Enter details of book #1
Enter book id: 101
Enter book name: LetUsC
Enter book author: Kanetkar
Enter book price: 350.20
Enter details of book #2
Enter book id: 102
Enter book name: ANSI_C
Enter book author: Balgurusamy
Enter book price: 300.78
Enter details of book #3
Enter book id: 103
Enter book name: OperatingSystem
Enter book author: Godbole
Enter book price: 295.67
*** Book 1 ***
Book Id: 101
Book Name: LetUsC
Book Author: Kanetkar
Book price: 350.200012
*** Book 2 ***
Book Id: 102
Book Name: ANSI_C
Book Author: Balgurusamy
Book price: 300.779999
*** Book 3 ***
Book Id: 103
Book Name: OperatingSystem
Book Author: Godbole
Book price: 295.670013