190 likes | 282 Views
Programming. Initializing Data of Struct Type. Ex. 10: Initialize Data of struct Type. By assignment during declaration; example : struct StudentRecord { // student record double totalgrade; char name[name_size], id[id_size]; int grade[no_grades]; };
E N D
Programming Initializing Data of Struct Type
Ex. 10: Initialize Data of struct Type • 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} };
Ex. 11: Initialize Data of struct Type 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. 12: Using struct Type in Functions • As parameters: both pass-by-value and pass-by-reference are supported. • As type of function: assembly of return value needed. Example of case 2: StudentRecord shrink_wrap( // a function with constchar 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
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} };
Sample input data file"class102.txt": Hello Kitty/ 1 11 1995 60 70 80Hello Catty/ 1 11 1995 60 70 80Minnie Mouse/ 2 12 1941 100 100 100Mickey Mouse/ 8 11 1941 50 50 50 Sample output: Enter number of students: 4Hello Kitty 9Hello Catty 9Minnie Mouse 64Mickey Mouse 64 Ex. 13: Big example
0 19 day Student name month birthday year score Classlist structure Classlist Class102; 0 49 clist Classlist no_tests size Date 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
Ex. 13: Big example #include <iostream>#include <fstream>#include <iomanip> using namesapce std;constint 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 studentsint no_tests; // number of completed testsint size;// number of students in clist};
int main(){int csize; Date today = {6, 11, 2009}; Classlist COMP102; cout << "Enter number of students: "; cin >> 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;}
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 Classlist COMP102 0 49 clist no_tests size Classlist structure // input file class102.txt 3 4
0 19 day name month birthday year score Classlist structure COMP102.Clist[0] Date Hello Kitty/ 1 11 1995 60 70 80
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.no_tests; count++) in >> 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);elseif(c.clist[index].birthday.month == today.month)if (c.clist[index].birthday.day <= today.day)return (today.year - c.clist[index].birthday.year);elsereturn (today.year - 1 - c.clist[index].birthday.year);elsereturn (today.year - 1 - c.clist[index].birthday.year); else{ cout << "Error: index out of range \n";return 0;} } // end-of-age
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
#include <iostream> #include <fstream> 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; }
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(constchar 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(); }
void list_record(constchar 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"); elseif(choice == 2) list_record("data.txt"); elseif (choice == 0) cout << "You have chosen to exit the program.\n" else cout << "Wrong choice, program aborted." << endl; }