Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure TDate struct TDate { int year, month, day; }; // Define a new data type.

Similar presentations


Presentation on theme: "Structure TDate struct TDate { int year, month, day; }; // Define a new data type."— Presentation transcript:

1 Structure TDate struct TDate { int year, month, day; }; // Define a new data type

2 //-------------------------------------------- // The function inputs data to a TDate struct // The input is in the format: mm/dd/yyyy. // Parameter: ( out ) //-------------------------------------------- void ReadDate(TDate& someDate) { char slash; cin >> someDate.month >> slash >> someDate.day >> slash >> someDate.year; } // Why using slash? // Input format: 11/21/2006 struct TDate { int year, month, day; };

3 //---------------------------------------- // The function displays a TDate struct // in the format mm/dd/yyyy. // Parameter: (In) //---------------------------------------- void WriteDate(const TDate& date) { cout << date.month << ‘/’ << date.day << ‘/’ << date.year; return; } void WriteDate(const TDate& date) { char slash = ‘/’; cout << date.month << slash << date.day << slash << date.year; return; }

4 Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; };

5 Accessing Members of Nested Structures StudentType s1; cin >> s1.TDate; // Valid? cin >> s1.DOB; // Valid? cin >> s1.DOB.day; // Valid? cin >> StudentType.DOB.day; // Valid?

6 //---------------------------------------------------- // The function inputs data to a StudentType struct // in the order: id, firstName, lastName, gpa, // DOB (month, day, year) and returns the struct. // Parameter: () //---------------------------------------------------- StudentType GetStudent() { StudentType s; cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; cin >> s.DOB.month >> s.DOB.day >> s.DOB.year; return s; }

7 //-------------------------------------------- // The function inputs data to a StudentType // struct in the order: id, firstName, // lastName, gpa, DOB (month, day, year). // Parameter: (out) //-------------------------------------------- void GetStudent(StudentType& s) { cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; cin >> s.DOB.month >> s.DOB.day >> s.DOB.year; return; }

8 //-------------------------------------------------- // The function inputs data to a StudentType struct // in the order: id, firstName, lastName, gpa, // DOB (using function). // void ReadDate(TDate& someDate) // Parameter: (out) //-------------------------------------------------- void GetStudent(StudentType& s) { cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; ReadDate(s); // Valid? ReadDate(DOB); // Valid? ReadDate(s.TDate); // Valid? ReadDate(s.DOB); // Valid? return; }

9 Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType { int numStudents; StudentType students[30]; };

10 Accessing Memebrs of Nested Structures SectionType cs143; InputSection(cs143); // Display the birth year of the last student cout << students[numStudents].TDate.year; // Valid? cout << cs143.students[numStudents - 1].TDate.year; // Valid? cout << cs143.students[cs143.numStudents - 1].TDate.year; // Valid? cout << cs143.students[cs143.numStudents - 1].DOB.year; // Valid?

11 Accessing Memebrs of Nested Structures SectionType cs143; InputSection(cs143); // Display the DOB of the first student // void WriteDate(const TDate& date) WriteDate(cs143.students[0]); // Valid? WriteDate(cs143.students[0].TDate); // Valid? WriteDate(cs143.students[0].DOB); // Valid?

12 Function Prototypes // Functions on TDate void ReadDate(TDate& someDate); void WriteDate(const TDate& date); // Functions on StudentType StudentType GetStudent() void DisplayStudent(const StudentType& s); void ModifyGPA(StudentType& s); // Functions on SectionType void InputSection(SectionType& sec); int IndexMaxGPA(const SectionType& sec); int IndexOfID(const SectionType& sec, string theID);


Download ppt "Structure TDate struct TDate { int year, month, day; }; // Define a new data type."

Similar presentations


Ads by Google