C++ struct Creates an array of 100 structures

Introduction

The code shows how member-arrays in structure are used.

#include <iostream>
using namespace std;
#include <stdio.h>
#include <ctype.h>
struct inventory/*from  w w  w .j a  v  a2s .c o  m*/
{
   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(book[total].title);
      cout << "What is the publication date? ";
      gets(book[total].pub_date);
      cout << "Who is the author? ";
      gets(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;
}



PreviousNext

Related