Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A structure derived data type that can contain elements with different data type

ID: 3563827 • Letter: A

Question

A structure derived data type that can contain elements with different data
types.
      Write a C program that will include the following:
        - A structure definition for BOOK, having elements: title, author, year
        - A main function that will
            + create an instance of the structure
            + pass this structure to a function
                   * the function will be able to input the values for all the elements of the structures for as many books as the user wants to enter.

Explanation / Answer

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

class stock

{

               char author[50];

               char title[50];

               char pub[50];

               double price;

               int numcopies;

   public:

               stock();

               int access_title(char a[]);

               void input();

               void getdata(int);

};

stock::stock()

{

                  char author[50]={"abc"};

                  char title[50]={"efg"};

                  char pub[50]={"hij"};

                  price=500;

                  numcopies=50;

}

int stock::access_title(char a[])

{

               if(strcmp(title,a))

                              return 0;

               else return 1;

}

void stock::getdata(int num)

{

               if(numcopies>=num)

                              cout<<" Cost of "<<num<<" books is Rs. "<<(price*num);

               else

                  cout<<" Sorry! These many copies are unavailable!";

}

void stock::input()

{

               cout<<" Title: ";

               gets(title);

               cout<<" Author:";

               gets(author);

               cout<<" Publisher:";

               gets(pub);

               cout<<" Prices:";

               cin>>price;

               cout<<" copies available:";

               cin>>numcopies;

}

void main()

{

   clrscr();

   stock obj[2];

   int n;

   char ttle[50];

   cout<<"Enter details of 3 books";

   for(int i=0;i<2;++i)

               obj[i].input();

   cout<<endl;

   cout<<" Enter title of required book ";

   gets(ttle);

   for(i=0;i<2;i++)

   {

               if(obj[i].access_title(ttle))

               {

                                             cout<<" How many copies? ";

                                             cin>>n;

                                             obj[i].getdata(n);

               }

               else

                                             cout<<" Book unavailable";

   }

   getch();

}

OUTPUT:

Enter details of 3 books

Title: Da Vinci Code

Author:Dan Brown

Publisher:Sun

Prices:455

copies available:300

Title: Harry Potter

Author:J K Rowling

Publisher:Bloomsbury

Prices:800

copies available:100

Enter title of required book

HarryPotter

Book available

How many copies? 20

Cost of 20 books is Rs. 16000


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote