C++ examples for Data Type:struct
Structure input with student data passed to functions.
#include <iostream> using namespace std; #include <string.h> #include <stdio.h> #include <iomanip> struct students fill_structs(struct students student_var); void pr_students(struct students student_var); struct students // A global structure. { char name[25]; int age;//from w w w .j ava 2 s . co m float average; }; // No memory reserved. void main() { students student1, student2; // Defines two local variables. student1 = fill_structs(student1); student2 = fill_structs(student2); // Print the data. cout << "\n\nHere is the student information you"; cout << " entered:\n\n"; pr_students(student1); pr_students(student2); return; } struct students fill_structs(struct students student_var) { fflush(stdin); // Clears input buffer for next input. cout << "What is student's name? "; gets_s(student_var.name); cout << "What is the student's age? "; cin >> student_var.age; cout << "What is the student's average? "; cin >> student_var.average; return (student_var); } void pr_students(struct students student_var) { cout << "Name: " << student_var.name << "\n"; cout << "Age: " << student_var.age << "\n"; cout << "Average: " << setprecision(2) << student_var.average << "\n"; return; }