Show book Information using Structure in C++


This program defines a Books struct that has four members: title, author, subject, and book_id. It then creates two instances of this struct: Book1 and Book2. The program initializes the members of Book1 and Book2 using strcpy to copy string values into each member. For example, strcpy(Book1.title,"Mathematics for IITJEE") copies the string "Mathematics for IITJEE" into the title member of Book1.

The program then sets the book_id member of each struct to an integer value. Finally, the program prints the values of each member of Book1 and Book2 using cout statements. Overall, this program is an example of how to define and use a struct in C++. It creates two instances of the Books struct, initializes their members, and prints their values.

Source Code

#include<iostream>
#include<cstring>
using namespace std;
struct Books
{
  char title[50];
  char author[50];
  char subject[100];
  int book_id;
};
int main()
{
  struct Books Book1;
  struct Books Book2;
  strcpy(Book1.title,"Mathematics for IITJEE");
  strcpy(Book1.author,"RD Sharma");
  strcpy(Book1.subject,"JEE Mathematics");
  Book1.book_id = 6495407;
  strcpy( Book2.title,"Physics");
  strcpy( Book2.author,"IE Irodov");
  strcpy( Book2.subject,"JEE Mechanical Physics");
  Book2.book_id=6495700;
  cout<<"Book 1 title : "<<Book1.title<<endl;
  cout<<"Book 1 author : "<<Book1.author<<endl;
  cout<<"Book 1 subject : "<<Book1.subject<<endl;
  cout<<"Book 1 id : "<<Book1.book_id<<endl;
  cout<<"Book 2 title : "<<Book2.title<<endl;
  cout<<"Book 2 author : "<<Book2.author<<endl;
  cout<<"Book 2 subject : "<<Book2.subject<<endl;
  cout<<"Book 2 id : "<<Book2.book_id<<endl;
  return 0;
}
To download raw file Click Here

Output

Book 1 title : Mathematics for IITJEE
Book 1 author : RD Sharma
Book 1 subject : JEE Mathematics
Book 1 id : 6495407
Book 2 title : Physics
Book 2 author : IE Irodov
Book 2 subject : JEE Mechanical Physics
Book 2 id : 6495700

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples