Upload
valentine-horn
View
212
Download
0
Embed Size (px)
Citation preview
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;
};
//----------------------------------------// 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;}
Nested Structures
struct TDate
{
int year, month, day;
};
struct StudentType
{
string id, firstName, lastName;
float gpa;
TDate DOB;
};
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?
//----------------------------------------------------// 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;}
//--------------------------------------------// 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;}
//--------------------------------------------------// 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;}
Nested Structuresstruct TDate
{
int year, month, day;
};
struct StudentType
{
string id, firstName, lastName;
float gpa;
TDate DOB;
};
struct SectionType
{
int numStudents;
StudentType students[30];
};
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?
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?
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);