CHAPTER 12 RECORDS ( Struct s)

24
CHAPTER 12 RECORDS (Structs)

description

CHAPTER 12 RECORDS ( Struct s). In this chapter, you will: Learn about records ( struct s) Examine various operations on a struct Explore ways to manipulate data using a struct Learn about the relationship between a struct and functions Discover how arrays are used in a struct - PowerPoint PPT Presentation

Transcript of CHAPTER 12 RECORDS ( Struct s)

Page 1: CHAPTER  12 RECORDS ( Struct s)

CHAPTER 12

RECORDS (Structs)

Page 2: CHAPTER  12 RECORDS ( Struct s)

In this chapter, you will: Learn about records (structs) Examine various operations on a struct Explore ways to manipulate data using a struct Learn about the relationship between a struct

and functions Discover how arrays are used in a struct Learn how to create an array of struct items

Page 3: CHAPTER  12 RECORDS ( Struct s)

Records (Struct) struct: A collection of fixed number of components in which components are

accessed by name is called a struct. The components may be of different types.

struct employeeType

{

string firstName;

string lastName;

string address1;

string address2;

double salary;

string deptID;

};

• struct is a definition not a declaration. No memory is allocated.• Memory is allocated when we declare variables.

Page 4: CHAPTER  12 RECORDS ( Struct s)

struct studentType{ string firstName, lastName; char courseGrade; int testScore, programmingScore; double GPA;};studentType newStudent; //variable declarationstudentType student;

Page 5: CHAPTER  12 RECORDS ( Struct s)

Accessing struct MembersIn C++, the . (dot) is an operator, called the member access operator.

newstudent.gpa = 0.0;newStudent.firstName = "John";newStudent.lastName = "Brown";cout << newStudent.firstName;

Page 6: CHAPTER  12 RECORDS ( Struct s)

AssignmentWe can assign the value of one struct variable to another struct variable of the same type using an assignment statement.

Page 7: CHAPTER  12 RECORDS ( Struct s)

student = newstudent;

Page 8: CHAPTER  12 RECORDS ( Struct s)

student = newstudent;

This statement is equivalent to the following statements:

student.firstName = newStudent.firstName;

student.lastName = newStudent.lastName;

student.courseGrade = newStudent.courseGrade;

student.testScore = newStudent.testScore;student.programmingScore = newStudent.programmingScore;

student.GPA = newStudent.GPA;

Page 9: CHAPTER  12 RECORDS ( Struct s)

Comparison struct variables must be compared member-wise.

To compare the values of student and newStudent, you must compare them member-wise, as follows:

if(student.firstName == newStudent.firstName &&

student.lastName == newStudent.lastName)

.

.

.

Page 10: CHAPTER  12 RECORDS ( Struct s)

•Input/Output• There are no aggregate input/output operations on struct.

• Data in a struct variable must be read one member at a time.

• Contents of a struct must be written one member at a time.

The statement

cout<<newStudent.firstName<<" "

<<newStudent.lastName<<" "

<<newStudent.courseGrade<<" "

<<newStudent.testScore<<" "

<<newStudent.programmingScore<<" "

<<newStudent.GPA<<endl;

outputs the content of the struct variable newstudent.

Page 11: CHAPTER  12 RECORDS ( Struct s)

struct Variables and Functions• A struct variable can be passed as a parameter either by value

or by reference.

• A function can return a value of the type struct.

Page 12: CHAPTER  12 RECORDS ( Struct s)

void readIn(studentType& student){ int score;

cin>>student.firstName>>student.lastName; cin>>student.testScore>>student.programmingScore; cin>>student.GPA;

score = newStudent.testScore + newStudent.programmingScore;

if(score >= 90) student.courseGrade = 'A'; else if(score >= 80) student.courseGrade = 'B'; else if(score >= 70) student.courseGrade = 'C'; else if(score >= 60) student.courseGrade = 'D'; else

student.courseGrade = 'F';}

Page 13: CHAPTER  12 RECORDS ( Struct s)

The statement

readIn(newStudent);

calls the function readIn.

• The function readIn stores the appropriate information in the variable newStudent.

Page 14: CHAPTER  12 RECORDS ( Struct s)

void printStudent(studentType student)

{

cout<<student.firstName<<" "

<<student.lastName<<" "

<<student.courseGrade<<" "

<<student.testScore<<" "

<<student.programmingScore<<" "

<<student.GPA<<endl;

}

Page 15: CHAPTER  12 RECORDS ( Struct s)
Page 16: CHAPTER  12 RECORDS ( Struct s)

int seqSearch(const listType& list, int searchItem)

{

int loc;

bool found = false;

for(loc = 0; loc < list.listLength; loc++)

if(list.elements[loc] == searchItem)

{

found = true;

break;

}

if(found)

return loc;

else

return –1;

}

Arrays in Structsconst arraySize = 1000;struct listType{ int elements[arraySize]; //array containing the list int listLength; //length of the list};

Page 17: CHAPTER  12 RECORDS ( Struct s)

Structs in Arrays Suppose there are 50 full time employees in a company. Print their monthly pay check and also keep track of how much

money has been paid year to date. Define an employee’s record.

struct employeeType

{

string firstName;

string lastName;

int personID;

string deptID;

double yearlySalary;

double monthlySalary

double yearToDatePaid;

double monthlyBonus;

};

Page 18: CHAPTER  12 RECORDS ( Struct s)

employeeType employees[50];

Page 19: CHAPTER  12 RECORDS ( Struct s)

The following C++ code loads the data into the employees’ array. We assume that initially yearToDatePaid is 0 and that the monthly

bonus is determined each month based on performance.

ifstream infile; //input stream variable assume //that employee.dat file has been openedfor(counter = 0; counter < 50; counter++){ infile>>employees[counter].firstName >>employees[counter].lastName >>employees[counter].SSN >>employees[counter].deptID >>employees[counter].yearlySalary; employees[counter].monthlySalary = employees[counter].yearlySalary/12; employees[counter].yearToDatePaid = 0.0; employees[counter].monthlyBonus = 0.0;}

Page 20: CHAPTER  12 RECORDS ( Struct s)

double payCheck; //variable to calculate the paycheck

for(counter = 0; counter < 50; counter++){ cout<<employees[counter].firstName<<" " <<employees[counter].lastName<<" ";

payCheck = employees[counter].monthlySalary + employees[counter].monthlyBonus;

employees[counter].yearToDatePaid = employees[counter].yearToDatePaid + payCheck;

cout<<setprecision(2)<<payCheck<<endl;}

Page 21: CHAPTER  12 RECORDS ( Struct s)

Structs within a struct

struct employeeType{

string firstname, middlename, lastname;string emplID;string address1, address2;string city;string state;string zip;string hiremonth, hireday, hireyear;string quitmonth, quitday, quityear;string phone;string cellphone;string fax;string pager;string email;string deptID;double salary;

};

Page 22: CHAPTER  12 RECORDS ( Struct s)

struct nameType{

string firstname;

string middlename;

string lastname;};

struct addressType{

string address1;string address2;string city;string state;string zip;

};

struct dateType{

string month;string day;string year;

};

struct contactType{

string phone;string cellphone;string fax;string pager;string email;

};

Page 23: CHAPTER  12 RECORDS ( Struct s)

struct employeeType

{

nameType name;

string emplID;

addressType address;

dateType hiredate;

dateType quitdate;

contactType contact;

string deptID;

double salary;

};

// variable declarationemployeeType newEmployee;

employeeType employees[100];

Page 24: CHAPTER  12 RECORDS ( Struct s)

newEmployee.salary = 45678.00;

newEmployee.name.first = "Mary";newEmployee.name.middle = "Beth";newEmployee.name.last = "Simmons";

cin>>newEmployee.name.first;

newEmployee.salary = newEmployee.salary * 1.05;

for(j = 0; j < 100; j++) cin>>employees[j].name.first >>employees[j].name.middle >>employees[j].name.last;