9. Chapter 11 Structured Data - compscisi/Programming-Fundamentals-1-SI GitHub Wiki
Example of a struct Declaration
More on structs
- MUST have a
;
after the closing}
- Struct names being with Upper case letter
- Struct declaration does not allocate memory or create variables
Use the dot (.) operator to refer to members of struct variables:
cin >> stu1.studentID;
getline(cin, stu1.name);
stu1.gpa = 3.75;
To display the contents of a struct variable, must display each field separately, using the dot operator:
cout << bill; // won’t work
cout << bill.studentID << endl;
cout << bill.name << endl;
cout << bill.yearInSchool;
cout << " " << bill.gpa;
Cannot compare struct variables directly:
if (bill == william) // won’t work
Instead, must compare on a field basis:
if (bill.studentID == william.studentID)