Create an array of structures and initialize their value - C++ Data Type

C++ examples for Data Type:struct

Description

Create an array of structures and initialize their value

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int NUMRECS = 5;  // maximum number of records
struct PayRec           // this is a global declaration
{
   int id;/*from  ww  w .j  a  v a  2  s.  c o m*/
   string name;
   double rate;
};
int main()
{
   int i;
   PayRec employee[NUMRECS] = {
      { 3, "A.", 6.72},
      { 4, "B.", 7.54},
      { 5, "D.", 5.56},
      { 6, "E.", 5.43},
      { 7, "G.", 8.72}
   };
   cout << endl;  // start on a new line
   cout << setiosflags(ios::left);   // left-justify the output
   for (i = 0; i < NUMRECS; i++)
      cout << setw(7)  << employee[i].id << setw(15) << employee[i].name << setw(6)  << employee[i].rate << endl;
   return 0;
}

Result


Related Tutorials