C++ examples for Data Type:struct
Structure input with student data.
#include <iostream> using namespace std; #include <string> #include <iomanip> #include <stdio.h> void main()/*from w ww . j a v a 2 s . c o m*/ { struct students { char name[25]; int age; float average; } student1, student2; // Get data for two students. cout << "What is first student's name? "; gets_s(student1.name); cout << "What is the first student's age? "; cin >> student1.age; cout << "What is the first student's average? "; cin >> student1.average; fflush(stdin); // Clear input buffer for next input. cout << "\nWhat is second student's name? "; gets_s(student2.name); cout << "What is the second student's age? "; cin >> student2.age; cout << "What is the second student's average? "; cin >> student2.average; // Print the data. cout << "\n\nHere is the student information you " << "entered:\n\n"; cout << "Student #1:\n"; cout << "Name: " << student1.name << "\n"; cout << "Age: " << student1.age << "\n"; cout << "Average: " << setprecision(2) << student1.average << "\n"; cout << "\nStudent #2:\n"; cout << "Name: " << student2.name << "\n"; cout << "Age: " << student2.age << "\n"; cout << "Average: " << student2.average << "\n"; return; }