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++

Run it Now
#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 << "\nEnter 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 :2
Enter Roll and Name of :1 :101 John
Enter Roll and Name of :2 :102 Jim
====== All Students Data is ====
Roll and Name of :1 101  John
Roll and Name of :2 102  Jim