1 / 24

CHAPTER 12 RECORDS ( Struct s)

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

Download Presentation

CHAPTER 12 RECORDS ( Struct s)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 12RECORDS (Structs)

  2. 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

  3. 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.

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

  5. Accessing structMembers In C++, the . (dot) is an operator, called the member access operator. newstudent.gpa = 0.0; newStudent.firstName = "John"; newStudent.lastName = "Brown"; cout << newStudent.firstName;

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

  7. student = newstudent;

  8. 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;

  9. 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) . . .

  10. 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.

  11. 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.

  12. 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'; }

  13. The statement readIn(newStudent); calls the function readIn. • The function readIn stores the appropriate information in the variable newStudent.

  14. void printStudent(studentType student) { cout<<student.firstName<<" " <<student.lastName<<" " <<student.courseGrade<<" " <<student.testScore<<" " <<student.programmingScore<<" " <<student.GPA<<endl; }

  15. Arrays in Structs const arraySize = 1000; struct listType { int elements[arraySize]; //array containing the list int listLength; //length of the list }; int seqSearch(const listType& list, int searchItem) { int loc; boolfound = false; for(loc = 0; loc < list.listLength; loc++) if(list.elements[loc] == searchItem) { found = true; break; } if(found) return loc; else return –1; }

  16. 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; };

  17. employeeType employees[50];

  18. 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 opened for(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; }

  19. 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; }

  20. 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; };

  21. struct dateType { string month; string day; string year; }; struct contactType { string phone; string cellphone; string fax; string pager; string email; }; struct nameType { string firstname; string middlename; string lastname; }; struct addressType { string address1; string address2; string city; string state; string zip; };

  22. struct employeeType { nameType name; string emplID; addressType address; dateType hiredate; dateType quitdate; contactType contact; string deptID; double salary; }; // variable declaration employeeType newEmployee; employeeType employees[100];

  23. 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;

More Related