Programming Initializing Data of Struct Type
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", " ", {80, 67, 34, 67} };
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
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
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", " ", scores}; has the same effect of: StudentRecord student1 = { 0.0, "CHAN Tai Man", " ", {80, 67, 34, 67} };
COMP102 Prog. Fundamentals: initialize struct type/ Slide 6 Ex. 13: Big example Sample input data file "class102.txt" : Hello Kitty/ Hello Catty/ Minnie Mouse/ Mickey Mouse/ Sample output: Enter number of students: 4 Hello Kitty 9 Hello Catty 9 Minnie Mouse 64 Mickey Mouse 64
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/ Hello Catty/ Minnie Mouse/ Mickey Mouse/
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 };
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; }
COMP102 Prog. Fundamentals: initialize struct type/ Slide 10 Classlist structure // input file class102.txt Classlist COMP clist no_tests size Hello Kitty/ Hello Catty/ Minnie Mouse/ Mickey Mouse/
COMP102 Prog. Fundamentals: initialize struct type/ Slide 11 Classlist structure COMP102.Clist[0] Hello Kitty/ name birthday score Date day month year
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(); }
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 c.clist[index].birthday.year); else return (today.year c.clist[index].birthday.year); else{ cout << "Error: index out of range \n"; return 0; } } // end-of-age
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: Pls enter
#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 [22]; }; int main(){ int pick_number = 1; do{ get_choice(pick_number); process_choice(pick_number); }while (pick_number); return 0; }
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'); }
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 "; cin >> top_gun. ; //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. << '\n'; } else cout << "Error...." << endl; outfile.close(); }
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(); }
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; }