Array of structs

database of records
//same employee struct as before
struct employee {
  string name;
  int emp_num;
  int dept;
  float salary;

//array of employees.  
//Each element is an employee struct variable.
employee company[max_employees];

int num_employees;		//virtual size

//element indexed by 2
company[2].emp_num = 37;
company[2].dept = 6;
company[2].salary = 30000;
company[2].name = "Smith";

if (company[i].dept == 8)
  cout << "Department 8 employee: " << company[i].name;

//jth employee in same department as ith employee
company[j].dept = company[i].dept;

//struct to struct copy
company[k] = company[i];

//initialize salaries of all employees to 0
for (i=0; i< num_employees; i++)  //loop over all employees
  company[i].salary = 0;
//find largest salaried employee
largest = 0;    //index of largest salaried employee
for (i=1; i< num_employees; i++)
  if (company[i].salary > company[largest].salary)
    largest = i;
cout << "Employee is " << company[largest].name;

//function to search an array of employees argument for a name
//returns index of element, else returns -1 if not found
int search_name (employee E[], int size, string name){
  int i=0;
  //loop while within bounds and not match
  while (i< size && E[i].name!=name)
  if (i < size)   //found name in array at index i
    return i;
    return -1;

int search_name (employee [], int, string);

employee company[max_employees];
employee LA_branch[max_branch];
ind = search_name(company,num_employees,"Smith");
if (ind != -1)
  cout << "We have a Smith";
  cout << "No Smiths here";

string name;
cout << "Enter name to search for: ";
cin >> name;
if ((ind=search_name(LA_branch,LA_size,name)) != -1)
  cout << "Employee information: " 
       << display_employee(LA_branch[ind]);
  cout << "No such employee in LA";

//function to display an employee
void display_employee (employee emp) {
  cout << << "\t" << setw(6) << emp.emp_num
       << setw(4) << emp.dept << setw(8) << emp.salary << endl;

//display all employees in the company array:
for (i=0; i< num_employees; i++)
  display_employee(company[i]);  //element is an employee struct

//function to count the number of employees whose
// salary is above a threshold value
int high_salary_count(employee workforce[], 
                      int num_employees, float salary){
  int i, count=0;
  for (i=0; i< num_employees; i++)
    if (workforce[i].salary > salary)
  return count;

int high_salary_count(employee [], int, float);

cout << "Enter figure: ";
cin >> salary;
cout << "Number of employees whose salary is above " 
     << salary << " is "
     << high_salary_count(company,num_employees,salary);

//Department x employees are going to LA.  
//Copy their records from company array to LA branch array.
cout << "Which department to copy: ";
cin >> dept;
cout << "There are " << LAsize << " department " 
     << dept << " employees";

int copy_dept (employee src[], int src_size, 
               employee dest[], int dept) {
  int i, j=0;
  for (i=0; i< src_size; i++)  //go thru src array
    if (src[i].dept == dept) {  //a dept x employee
      dest[j] = src[i];    //copy to dest array
      j++;                 //increment count of x employees
  return j;    //return size of dest array == #dept x employees

//function to delete dept x employees from an array of employees

cout << "Enter department to be terminated: ";
cin >> dept;

//now company array has no more dept x employees.  Its size is that 
//much smaller.

void delete_dept (employee E[], int &size, int dept) {
  int i=0;
  while (i < size)            //until end of array
    if (E[i].dept == dept) {  //a dept x employee
      //logically remove by overwriting with the struct 
      //  at the end of the array
      E[i] = E[size-1];      //the last employee      
      size--;      //since just moved the last one, 
                   //decrement the size
    }  //since it may also be a dept x employee, 
       //  it needs to be looked at

    else         //not a dept x employee
      i++;       //so go to next employee in array


Structure suitable for celebrities:

struct celeb {
  string name;
  string sign;
  date birthday; //A member can be any type, 
                  //including another struct.

celeb c1;
cin >>;
cl.birthday.year = 1960;

//array of celeb structs
celeb stars[max_stars];

cout << "Enter number of celebrities: ";
cin >> num_stars;
for (i=0; i< num_stars; i++) {
  cout << "Enter name: ";
  cin >> stars[i].name;
  cout << "Enter sign: ";
  cin >> stars[i].sign;
  get_date(stars[i].birthday);  //date reference argument

//count number of Pisces
pisces = 0;
for (i=0; i< num_stars; i++) 
  if (stars[i].sign=="Pisces")
cout << "Number of Pisces is " << pisces;

//count number of stars older than me
date my_birthday;
cout << "Enter your birthday: ";
older = 0;
for (i=0; i< num_stars; i++) 
  if (datecmp(my_birthday,stars[i].birthday) == 1)
cout << "Number of stars older than you is " << older;


//function to count number of celebrities of a particular sign
int num_starsigns (celeb stars[], int size, string sign) {
  int i, count = 0;
  for (i=0; i< size; i++)
    if (stars[i].sign == sign)
  return count++;

Exercise: function to count number of each sign.  Use array of
signcounts and array of sign strings.


//Structure suitable for parts database
struct part {
  int part_id;
  string description;
  float price;
  int quantity;
  char reorder;   //Y or N flag

part bolt37, nut, nail;
part parts[max_parts];

get_part_info(nut);     //part reference argument

//2D array of part structs.  Row is a site/branch
part corporate[max_branches][max_parts];


void get_part_info (part &item) {
  cout << "Enter part id: ";
  cin >> item.part_id;
  cout << "Enter description: ";
  cin >> item.description;
  cout << "Enter price: ";
  cin >> item.price;
  cout << "Enter quantity in inventory: ";
  cin >> item.quantity;

//reorder is calculated 

getline (char_array,  max_number_of_chars_to_input_from_line) 

part struct field sizes: part_id 4 bytes, say average
description is 80 bytes, 
price 4 bytes, quantity 4 bytes, reorder 1 byte.  
So a part variable is 93 bytes.  Use sizeof to determine to actual
size, which may be larger due to padding.
Say max_parts is 1000, then parts array is 1000*93=93000 bytes
Say max_branches is 100, then corporate 2D array is 
  100*1000*93=9,300,000 bytes=9 MB


Structure for playing cards:

struct card {
  int face;    //1=Ace, 2,3,...10,11=Jack,12=Queen,13=King
  char suit;  //C, D, H, S

//deck of cards
card deck[52];

init_deck(deck);  //the 52 different cards

shuffle(deck);    //repeatedly swap random pairs

card hand[hand_size];
deal_hand(deck,hand);  //copy/move from deck to hand arrays

display_hand(hand);   //convert internal to external

score_hand(hand);   //poker: straight, full house, etc


struct date {
  int year;
  int month;
  int day;

struct personal {
  string name;
  date birthday;

struct student {
  personal pers;
  int id;
  char grades[max_grades];

student stu1;

//Examples of accessing members: = 12345;
stu1.grades[3] = 'B'; = "Smith";
stu1.pers.birthday.year = 1960;

cout << "How many grades for this student? ";
cin >> num;
for (i=0; i> num_students;       	//less than max_pupils
for (i=0; i< num_students; i++)
  cin >> school[i];

File of struct "records".

Actually, text files that appear to consist of records.
Outputting to file is exactly like outputting to monitor with cout.
Inputting from file is exactly like inputting from keyboard with cin.
//function to write array of employee structs to a file
void write_employees (employee E[], int size, string fname) {
  int i;
  ofstream empf;;

  if (!empf)
    cerr << "ERROR opening " << fname << endl;
    for (i=0; i< size; i++)
      empf << E[i].emp_num << setw(maxname+2) << E[i].name
           << setw(4) << E[i].dept << "  " << E[i].salary << endl;

File will have format:
493      Smith   6  30000
62       Jones  21  25000

//function to read "records" from file with same format to
// array of employees
void read_employees (employee E[], int &size, string fname) {
  ifstream empf;;
  if (!empf)
    cerr << "ERROR opening " << fname << endl;
  else {
    empf >> E[size].emp_num >> E[size].name >> E[size].dept 
         >> E[size].salary;
    while (empf) {
      empf >> E[size].emp_num >> E[size].name >> E[size].dept 
           >> E[size].salary;
Program to maintain dictionary of words diction.cpp

The end.
See you in 240.

©David Wills