Creates an array of 100 structures. The code shows how member-arrays in structure are used. - C++ Data Type

C++ examples for Data Type:struct

Description

Creates an array of 100 structures. The code shows how member-arrays in structure are used.

Demo Code

#include <iostream>
using namespace std;
#include <stdio.h>
#include <ctype.h>
struct inventory/*from w ww .  j  av a2s  .c  om*/
{
   char title[25];                   // Book's title.
   char pub_date[19];                // Publication date.
   char author[20];                  // Author's name.
   int num;                          // Number in stock.
   int on_order;                     // Number on order.
   float retail;                     // Retail price.
};
void main()
{
   inventory book[100];
   int total=0;               // Total books in inventory.
   int ans;
   do     // This program enters data into the structures.
   {
      cout << "Book #" << (total+1) << ":\n", (total+1);
      cout << "What is the title? ";
      gets_s(book[total].title);
      cout << "What is the publication date? ";
      gets_s(book[total].pub_date);
      cout << "Who is the author? ";
      gets_s(book[total].author);
      cout << "How many books of this title are there? ";
      cin >> book[total].num;
      cout << "How many are on order? ";
      cin >> book[total].on_order;
      cout << "What is the retail price? ";
      cin >> book[total].retail;
      fflush(stdin);
      cout << "\nAre there more books? (Y/N) ";
      ans=getchar();
      fflush(stdin);            // Discard carriage return.
      ans=toupper(ans);         // Convert to uppercase.
      if (ans=='Y')
      {
         total++;
         continue;
      }
   } while (ans=='Y');
   return;
}

Result


Related Tutorials