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

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

Embed Size (px)

Citation preview

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

Structure TDate

struct TDate

{

int year, month, day;

};

// Define a new data type

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

//--------------------------------------------// 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;

};

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

//----------------------------------------// 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;}

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

Nested Structures

struct TDate

{

int year, month, day;

};

struct StudentType

{

string id, firstName, lastName;

float gpa;

TDate DOB;

};

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

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?

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

//----------------------------------------------------// 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;}

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

//--------------------------------------------// 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;}

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

//--------------------------------------------------// 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;}

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

Nested Structuresstruct TDate

{

int year, month, day;

};

struct StudentType

{

string id, firstName, lastName;

float gpa;

TDate DOB;

};

struct SectionType

{

int numStudents;

StudentType students[30];

};

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

Accessing Memebrs of Nested Structures

SectionType cs143;

InputSection(cs143);

// Display the birth year of the last studentcout << 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?

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

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?

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

Function Prototypes

// Functions on TDatevoid ReadDate(TDate& someDate);void WriteDate(const TDate& date);

// Functions on StudentTypeStudentType GetStudent()void DisplayStudent(const StudentType& s);void ModifyGPA(StudentType& s);

// Functions on SectionTypevoid InputSection(SectionType& sec);int IndexMaxGPA(const SectionType& sec);int IndexOfID(const SectionType& sec, string theID);