C++ Program using structure and array


In this program, You will learn how to implement structure and array in C++.


struct Student st[size];

Example: How to implement structure and array in C++.

#include<iostream>
using namespace std;

struct Student {
    int roll;
    char name[100];
};

int main() {

    int k, i;

    cout << "Enter How many Students:";
    cin>>k;

    Student st[k];

    for (i = 0; i < k; i++) {
        cout << "Enter Roll and Name of :" << i + 1 << " :";
        cin >> st[i].roll;
        cin.getline(st[i].name, 100);
    }

    cout << "\n====== All Students Data is ====";
    for (i = 0; i < k; i++) {
        cout << "\nRoll and Name of :" << i + 1 << " " << st[i].roll << " " << st[i].name;
    }

    return 0;
}

Output:

Enter How many Students:3
Enter Roll and Name of :1 :101 xiith1
Enter Roll and Name of :2 :102 xiith2
Enter Roll and Name of :3 :103 xiith3

====== All Students Data is ====
Roll and Name of :1 101  xiith1
Roll and Name of :2 102  xiith2
Roll and Name of :3 103  xiith3