Reading and writing student data demonstrating the an array of struct data type


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Reading and writing student data using an array of struct data type in C++

To show: How to define, declare and use an array of struct in C++ program


// An array structure of student information

#include <iostream>

#include <cstdlib>

using namespace std;


// define the student structure

struct student


char id[6]; // student id number, max. 5 integer number

char name[50]; // student name, max 49 characters

char gender; // student gender Male or Female

int age; // student age



void main(void)


// declaring array of 10 element of structure type and some of the element also are arrays

struct student stud[10];

int i = 0;


cout<<"Keying in student data and then display\n";


cout<<"Enter student data\n";


for(i=0; i<2; i++)


// Storing the data

cout<<"\nID number (4 integer number) student #"<<i<<cin>>stud[i].id;

cout<<"First name student #"<<i<<cin>>stud[i].name;

cout<<"Gender (M or F) student #"<<i<<cin>>stud[i].gender;

cout<<"Age student #"<<i<<cin>>stud[i].age;


cout<<"\n----------Display the data---------\n";

cout<<"You can see that the data storage\n";

cout<<"has been reserved for the structure!\n";



for(i=0; i<2; i++)


// Displaying the stored data

cout<<"\nID number student # "<<i<<": "<<stud[i].id;

cout<<"\nFirst name student # "<<i<<": "<<stud[i].name;

cout<<"\nGender student # "<<i<<": "<<stud[i].gender;

cout<<"\nAge student # "<<i<<": "<<stud[i].age<<"\n";




Output example:


Keying in student data and then display


Enter student data

ID number (4 integer number) student #0: 5371

First name student #0: Lenny

Gender (M or F) student #0: F

Age student #0: 25

ID number (4 integer number) student #1: 2845

First name student #1: Ulrich

Gender (M or F) student #1: M

Age student #1: 45

----------Display the data---------

You can see that the data storage

has been reserved for the structure!


ID number student # 0: 5371

First name student # 0: Lenny

Gender student # 0: F

Age student # 0: 25

ID number student # 1: 2845

First name student # 1: Ulrich

Gender student # 1: M

Age student # 1: 45

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index