Download presentation
Presentation is loading. Please wait.
Published byChad Pierce Arnold Modified over 9 years ago
1
Programming Initializing Data of Struct Type
2
COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By assignment during declaration; example : struct StudentRecord { // student record double totalgrade; char name[name_size], id[id_size]; int grade[no_grades]; }; StudentRecord student1 = { 0.0, "CHAN Tai Man", "12345678", {80, 67, 34, 67} };
3
COMP102 Prog. Fundamentals: initialize struct type/ Slide 3 Initialization of a “ nested ” structure: struct Point { int x, y; }; //defines the coordinates of a point struct Triangle { Point vertex[3]; double area; }; //defines a triangle Triangle tlist[2] = { {{{0, 0}, {1, 0}, {0, 1}}, 0.5}, // 1st Triangle {{{2, 0}, {3, 0}, {3, 1}}, 0.5} // 2nd Triangle }; Ex. 11: Initialize Data of struct Type
4
COMP102 Prog. Fundamentals: initialize struct type/ Slide 4 Ex. 12: Using struct Type in Functions 1. As parameters: both pass-by-value and pass-by-reference are supported. 2. As type of function: assembly of return value needed. Example of case 2: StudentRecord shrink_wrap( // a function with const char sname[], // name shrink_wrap of const char sid[], // type StudentRecord const int sgrade[]) { StudentRecord temp; temp.totalgrade = 0; strcpy (temp.name, sname); strcpy (temp.id, sid); for (int index=0; index<no_grades; index++) temp.grade[index] = sgrade[index]; return temp; } // Defines an initialization function
5
COMP102 Prog. Fundamentals: initialize struct type/ Slide 5 Ex. 12: Using struct Type in Functions StudentRecord student1; int scores[4] = {80, 67, 34, 67}; student1 = shrink_wrap ("CHAN Tai Man", "12345678", scores}; has the same effect of: StudentRecord student1 = { 0.0, "CHAN Tai Man", "12345678", {80, 67, 34, 67} };
6
COMP102 Prog. Fundamentals: initialize struct type/ Slide 6 Ex. 13: Big example Sample input data file "class102.txt" : Hello Kitty/ 1 11 1995 60 70 80 Hello Catty/ 1 11 1995 60 70 80 Minnie Mouse/ 2 12 1941 100 100 100 Mickey Mouse/ 8 11 1941 50 50 50 Sample output: Enter number of students: 4 Hello Kitty 9 Hello Catty 9 Minnie Mouse 64 Mickey Mouse 64
7
COMP102 Prog. Fundamentals: initialize struct type/ Slide 7 Classlist structure Classlist Class102; Classlist 049 clist no_tests size Student 019 name birthday score Date day month year Hello Kitty/ 1 11 1995 60 70 80 Hello Catty/ 1 11 1995 60 70 80 Minnie Mouse/ 2 12 1941 100 100 100 Mickey Mouse/ 8 11 1941 50 50 50
8
COMP102 Prog. Fundamentals: initialize struct type/ Slide 8 Ex. 13: Big example #include #include #include using namesapce std; const int name_size = 20, num_score= 5, class_size=50; struct Date{ int day; int month; int year; }; struct Student{ char name[name_size]; Date birthday; int score[num_score]; }; struct Classlist{ Student clist[class_size]; // list of max 50 students int no_tests; // number of completed tests int size; // number of students in clist };
9
int main(){ int csize; Date today = {6, 11, 2009}; Classlist COMP102; cout > csize; COMP102.no_tests = 3; COMP102.size = csize; in_class(COMP102); for (int index=0; index < csize; index++) cout << setw(25) << setiosflags(ios::left) << COMP102.clist[index].name << setw(25) << setiosflags(ios::left) << age(today, index, COMP102) << endl; return 0; }
10
COMP102 Prog. Fundamentals: initialize struct type/ Slide 10 Classlist structure // input file class102.txt Classlist COMP102 049 clist no_tests size Hello Kitty/ 1 11 1995 60 70 80 Hello Catty/ 1 11 1995 60 70 80 Minnie Mouse/ 2 12 1941 100 100 100 Mickey Mouse/ 8 11 1941 50 50 50 3 4
11
COMP102 Prog. Fundamentals: initialize struct type/ Slide 11 Classlist structure COMP102.Clist[0] Hello Kitty/ 1 11 1995 60 70 80 019 name birthday score Date day month year
12
void in_class(Classlist & Class){ // This function reads in the student records from the // input file stream and stores them in Class. ifstream in; in.open(“comp102.txt”); char temp; for (int index=0; index < Class.size; index++){ //read in the name in.getline(Class.clist[index].name, 19, '/'); in >> Class.clist[index].birthday.day; in >> Class.clist[index].birthday.month; in >> Class.clist[index].birthday.year; for (int count=0; count > Class.clist[index].score[count]; in.get(temp); // read carriage return } in.close(); }
13
int age(Date today, int index, Classlist c){ // This function computes the age in years for the // student at the index position in c. int csize; csize = c.size; if (index < csize) if (c.clist[index].birthday.month < today.month) return (today.year - c.clist[index].birthday.year); else if(c.clist[index].birthday.month == today.month) if (c.clist[index].birthday.day <= today.day) return (today.year - c.clist[index].birthday.year); else return (today.year - 1 - c.clist[index].birthday.year); else return (today.year - 1 - c.clist[index].birthday.year); else{ cout << "Error: index out of range \n"; return 0; } } // end-of-age
14
COMP102 Prog. Fundamentals: initialize struct type/ Slide 14 Ex. 14: Using struct Type in Functions The next example adds student records into a file, and can list all the student records in that file: sample output: Please enter choice: 0 - Exit 1 - Add a record 2 - List all records 1 Pls enter name: Mickey Mouse Pls enter id: 12345678 Pls enter email: mickey@waltdisney.com
15
#include using namesapce std; void get_choice(int& pick_number); void process_choice(int pick_number); struct student_info{ char name[22]; char id[9]; char email[22]; }; int main(){ int pick_number = 1; do{ get_choice(pick_number); process_choice(pick_number); }while (pick_number); return 0; }
16
void get_choice(int& choice) { do { cout << "Please enter choice:" << endl; cout << " 0 - Exit" << endl; cout << " 1 – Add a record" << endl; cout << " 2 - List all records" << endl; cin >> choice; } while (choice != 0 && choice != 1 && choice != 2); cin.ignore(256, '\n'); }
17
void add_record(const char student_record[]){ ofstream outfile; struct student_info top_gun; cout << "Pls enter name: "; cin.getline(top_gun.name, 21); cout << "Pls enter id: "; cin >> top_gun.id; cout << "Pls enter email: "; cin >> top_gun.email; //ios::app - opening file for appending outfile.open(student_record, ios::app); if(outfile){ // If no error occurred while opening file outfile << top_gun.name << '\t'; outfile << top_gun.id << '\t'; outfile << top_gun.email << '\n'; } else cout << "Error...." << endl; outfile.close(); }
18
void list_record(const char student_record[]){ ifstream infile; char x; //ios::in - opening file for input infile.open(student_record); infile.getline(temp,80); while(!infile.eof()){ cout << temp << endl; infile.getline(temp,80); } infile.close(); }
19
void process_choice(int choice){ if (choice == 1) add_record("data.txt"); else if(choice == 2) list_record("data.txt"); else if (choice == 0) cout << "You have chosen to exit the program.\n" else cout << "Wrong choice, program aborted." << endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.