individual structure members are accessed through the use of a period, generally
called the dot operator. For example, the following code will assign the value
60.0 to the first_exam field of the structure variable varA declared earlier
varA.first_exam=60.0;
The structure variable name, followed by a period and the member name, refers to
that member. All structure elements are accessed in the same way, the general form
is
structure_varname.member_name
If you wish to access the individual elements of the array grade.name, you can
index name. For example you can print the contents of grade.name one
character at a time by using this code
int t;
for(t=0;grade.name[t];t++)
cout <<grade.name[t];
Structure may be arrayed. To declare an array of structures, you must first define
a structure, then declare an array of its type. For example to declare 100-element
array of structures of type grade you would write
grade eml3002[28];// defines structure for 28 students in eml3002
To access a specific structure, you must index the structure name. For example to
print second exam of second student you would write
cout<<eml3002[1].second_exam;
Like all array variables, arrays of structures begin their indexing at zero.
Example
/* A simple grade record program */
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
const int size=2; /*defines the number of student in a class, const modifier fixes the
value of size through out the program */
struct grades{
char name[20]; // Name of a student with 20 characters
int ssn; //Social security numbers
double first_exam;// first exam
double second_exam;//second exam
float homework;//homework
double final;// final
} eml3002[size];
/* The program will provide these three options
1. Enter information
2. Display information
3. Modify information */
void enter(), init_list(), display(); // prototypes of functions
void update(), input(int i);// prototypes of functions
int menu();//prototype of functions
main()
{
char choice; //defines a character
init_list(); //initialize the structure array
for(;;){
choice=menu();// menu() displays the options and returns the user selection
switch(choice){
case 'e':enter();
break;
case 'd':display();
break;
case 'u':update();
break;
case 'q':return 0;
}
}
}
// Initializing the eml3002 array
{
int t;
// for space use the null character '0'
for(t=0;t<size;t++) *eml3002[t].name= '0';
}
// Get a menu selection
menu()
{
char ch;
cout <<'n';
do{
cout <<"(E)ntern";
cout <<"(D)isplayn";
cout <<"(U)pdaten";
cout <<"(Q)uitnn";
cout <<"Choose one:";
cin >>ch;
} while(!strchr("eduq",tolower(ch)));
return tolower(ch);
}
/* The program uses another C++ library functions strchr( char *str, char ch). This function searches
the string pointed by str for an occurance of the character in ch. If no match is
found a null is returned*/
// Enter items into the list
void enter()
{
int i;
// find the first free structure
for (i=0;i<size;i++)
if(!*eml3002[i].name)break;
// i will equal size if the list is full
if(i==size){
cout <<"List fulln";
return;
}
input(i);
}
//Input the information
void input(int i)
{
char str[80];
// enter the information
cout <<"Name: ";
cin >>eml3002[i].name;// last name
cout << "SSN :";// last 4 digits
cin >> eml3002[i].ssn;
cout << "First Exam: ";
cin >> eml3002[i].first_exam;// double
cout <<" Second Exam: ";
cin >>eml3002[i].second_exam;
cout <<"Homework: ";
cin >> eml3002[i].homework;
cout << "Final: ";
cin >> eml3002[i].final;
}
// modify an exiting grade
void update()
{
int i;
char mname[80];
cout << "Enter name:";
cin >>mname;
for(i=0;i<size;i++)
if(!strcmp(mname,eml3002[i].name)) break;// uses a library function
if(i==size){
cout << "name not foundn";
return;
}
cout << "Enter new information. n";
input(i);
}
// Display the list
void display()
{
int t;
for(t=0;t<size;t++){
if(*eml3002[t].name){
cout << eml3002[t].name <<'n';
cout << "SSN :" <<eml3002[t].ssn <<'n';
cout << "First Exam " <<eml3002[t].first_exam <<'n';
cout << "Second Exam " <<eml3002[t].second_exam<<'n';
cout <<"Homework " << eml3002[t].homework<<'n';
cout <<"Final "<< eml3002[t].final<<'n';
}
}
}
Activity For this activity the