C++ struct array

Suppose that your program is to deal with data of 20 students. Each student contains identification number, name, address, and department id. One solution is to create a structure that has those fields as its members. Then you will create an array of 20 structure objects.

#include <iostream>
#include <conio.h>
using namespace std;
struct Student{ //define Student structure

    //data members
     int Stid;
     string Stname;
     string Staddress;
     int Deptid;
     };
    

int main(){
   Student st[20];//array of 20 structures
   //to assign data to all elements of the array
   //you need a loop
   //first student data, just for example
   st[0].Stid=100113;
   st[0].Stname="Ly Meng";
   st[0].Staddress="#23,St.Monivong, Phnom Penh";
   st[0].Deptid=13;
   //read data from structure
   cout<<"ID:"<<st[0].Stid<<endl;
   cout<<"Name:"<<st[0].Stname<<endl;
   cout<<"Address:"<<st[0].Staddress<<endl;
   cout<<"Dept id:"<<st[0].Deptid<<endl;
   getch();
   return 0;
}

Posted by: C++ struct array | post date: 01-26-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.