arrow
arrow

Structure

Syntax of Structure


                            
                                #include<iostream>
                                    #include<string>
                                    using namespace std;
                                    struct student{
                                       string name;
                                       string roll;
                                       int age;
                                       float cgpa;
                                    };
                                    int main()
                                    {
                                        student st1;
                                       cout<<"Enter the name ,roll #,age and cgpa of the student 1:"<<endl;
                                       getline(cin,st1.name);
                                       getline(cin,st1.roll);
                                       cin>>st1.age;
                                       cin>>st1.cgpa;
                                    }
                                    
                            
                        

Structure of the 5 students


                        
                            #include <iostream>
                                #include <string>
                                using namespace std;
                                struct student
                                {
                                    int roll;
                                    int age;
                                    int marks;
                                    string name;
                                };
                                int main()
                                {
                                    student st;
                                    student st2;
                                    student st3;
                                    student st4;
                                    student st5;
                                    cout << "Enter the name of the student 1:" << endl;
                                    getline(cin, st.name);
                                    cout << "Enter the roll # of the student 1:" << endl;
                                    cin >> st.roll;
                                    cout << "Enter the age of the student 1:" << endl;
                                    cin >> st.age;
                                    cout << "Enter the marks of the student 1:" << endl;
                                    cin >> st.marks;
                                    cin.ignore();
                                    cout<<endl;
                                    cout << "Enter the name of the student 2:" << endl;
                                    getline(cin, st2.name);
                                    cout << "Enter the roll # of the student 2:" << endl;
                                    cin >> st2.roll;
                                    cout << "Enter the age of the student 2:" << endl;
                                    cin >> st2.age;
                                    cout << "Enter the marks of the student 2:" << endl;
                                    cin >> st2.marks;
                                    cin.ignore();
                                    cout<<endl;
                                    cout << "Enter the name of the student 3:" << endl;
                                    getline(cin, st3.name);
                                    cout << "Enter the roll # of the student 3:" << endl;
                                    cin >> st3.roll;
                                    cout << "Enter the age of the student 3:" << endl;
                                    cin >> st3.age;
                                    cout << "Enter the marks of the student 3:" << endl;
                                    cin >> st3.marks;
                                    cin.ignore();
                                    cout<<endl;
                                    cout << "Enter the name of the student 4:" << endl;
                                    getline(cin, st4.name);
                                    cout << "Enter the roll # of the student 4:" << endl;
                                    cin >> st4.roll;
                                    cout << "Enter the age of the student 4:" << endl;
                                    cin >> st4.age;
                                    cout << "Enter the marks of the student 4:" << endl;
                                    cin >> st4.marks;
                                    cin.ignore();
                                    cout<<endl;
                                    cout << "Enter the name of the student 5:" << endl;
                                    getline(cin, st5.name);
                                    cout << "Enter the roll # of the student 5:" << endl;
                                    cin >> st5.roll;
                                    cout << "Enter the age of the student 5:" << endl;
                                    cin >> st5.age;
                                    cout << "Enter the marks of the student 5:" << endl;
                                    cin >> st5.marks;
                                    cout << "_______________________________________" << endl;
                                    cout << "The name of the student 2 = " << st2.name << endl;
                                    cout << "The roll # of the student 2 = " << st2.roll << endl;
                                    cout << "The age of the student 2 = " << st2.age << endl;
                                    cout << "The marks of the student 2 = " << st2.marks << endl;
                                    return 0;
                                }
                        
                    

Create an array of 3 students. Hardcode the array data and ask user to ask an ID. Take ID from user and search from the students array and when the ID matches print the data of respective student.


                        
                            #include <iostream>
                                #include <string>
                                using namespace std;
                                struct student
                                {
                                    string name;
                                    string roll;
                                    int session;
                                    int id;
                                };
                                int main()
                                {
                                    student st[3];
                                    st[0].name = "Muhib";
                                    st[1].name = "Ali";
                                    st[2].name = "Hamza";
                                    st[0].roll = "BSEF21M540";
                                    st[1].roll = "BSEF21M541";
                                    st[2].roll = "BSEF21M543";
                                    st[0].session = 2021;
                                    st[1].session = 2021;
                                    st[2].session = 2021;
                                    st[0].id = 1;
                                    st[1].id = 2;
                                    st[2].id = 3;
                                    int n;
                                    cout << "Enter the id of the student that you want to know:" << endl;
                                    cin >> n;
                                    for (int i = 0; i < 3; i++)
                                    {
                                        if (st[i].id == n)
                                        {
                                            cout << "____________________________________" << endl;
                                            cout << "The name of the student " << i + 1 << " =" << st[i].name << endl;
                                            cout << "The roll # of the student " << i + 1 << " =" << st[i].roll << endl;
                                            cout << "The session of the student " << i + 1 << " =" << st[i].session << endl;
                                            cout << "The id of the student " << i + 1 << " =" << st[i].id << endl;
                                        }
                                    }
                                
                                
                                    return 0;
                                }
                        
                    

Nested Structure


                        
                            #include<iostream>
                                using namespace std;
                                struct ra{
                                   string hi;
                                };
                                struct date{
                                    int min;
                                    int hr;
                                    int sec;
                                    ra gg;
                                };
                                struct student{
                                    string name;
                                    int roll;
                                    int sem;
                                    float cgpa;
                                    date dob;
                                };
                                int main(){
                                  student st1;
                                  cout<<"Enter the name of the student:"<<endl;
                                  getline(cin,st1.name);
                                  cout<<"Enter the Roll number of the student:"<<endl;
                                  cin>>st1.roll;
                                  cout<<"Enter the semister of the student:"<<endl;
                                  cin>>st1.sem;
                                  cout<<"Enter the cpga of the stusent:"<<endl;
                                  cin>>st1.cgpa;
                                  cout<<"Enter the hours:"<<endl;
                                  cin>>st1.dob.hr;
                                  cout<<"Enter the minutes:"<<endl;
                                  cin>>st1.dob.min;
                                  cout<<"Enter the seconds:"<<endl;
                                  cin>>st1.dob.sec;
                                  cout<<"say hi"<<endl;
                                  cin>>st1.dob.gg.hi;
                                  cout<<"____________________________________________________________"<<endl;
                                  cout<<"The name of the student is :"<<st1.name<<endl;
                                  cout<<"The Roll# of the student is :"<<st1.roll<<endl;
                                  cout<<"The Semister of the student is :"<<st1.sem<<endl;
                                  cout<<"The CGPA of the student is :"<<st1.cgpa<<endl;
                                  cout<<"The Hours of the student is :"<<st1.dob.hr<<endl;
                                  cout<<"The Minutes of the student is :"<<st1.dob.min<<endl;
                                  cout<<"The Seconds of the student is :"<<st1.dob.sec<<endl;
                                  cout<<"The Hi of the student is :"<<st1.dob.gg.hi<<endl;
                                
                                    return 0;
                                }
                                
                                
                        
                    

Struture with arrays to get the unlimited data


                        
                            #include <iostream>
                                #include<cstring>
                                using namespace std;
                                struct address
                                {
                                    string country;
                                    string division;
                                    string district;
                                    string city;
                                    string postelAddress;
                                };
                                struct data
                                {
                                    char name[25];
                                    int age;
                                    string rollNo;
                                    char gender[15];
                                    int hostel_no;
                                    int room_no;
                                    char department[100];
                                    int semister;
                                    address addr;
                                };
                                int main()
                                {
                                    data st[4];
                                    string n;
                                    for (int i = 0; i < 4; i++)
                                    {
                                        cout << "Enter the name of the student no " << i + 1 << " :" << endl;
                                        cin.getline(st[i].name, 25);
                                        cout << "Enter the roll number of the student no " << i + 1 << " :" << endl;
                                        getline(cin, st[i].rollNo);
                                        cout << "Enter the gender of the student no " << i + 1 << " :" << endl;
                                        cin.getline(st[i].gender, 15);
                                        cout << "Enter the age of the student no " << i + 1 << " :" << endl;
                                        cin >> st[i].age;
                                        cin.ignore();
                                        cout << "Enter the department of the student no " << i + 1 << " :" << endl;
                                        cin.getline(st[i].department, 100);
                                        cout << "Enter the semister of the student no " << i + 1 << " :" << endl;
                                        cin >> st[i].semister;
                                        cout << "Enter hostel number of the student no " << i + 1 << " :" << endl;
                                        cin >> st[i].hostel_no;
                                        cout << "Enter the room number of the student no " << i + 1 << " :" << endl;
                                        cin >> st[i].room_no;
                                        cin.ignore();
                                        cout << "Enter the address as:" << endl;
                                        cout << "Enter the country:";
                                        getline(cin, st[i].addr.country);
                                        cout << "Enter the division:";
                                        getline(cin, st[i].addr.division);
                                        cout << "Enter the district:";
                                        getline(cin, st[i].addr.district);
                                        cout << "Enter the city:";
                                        getline(cin, st[i].addr.city);
                                        cout << "Enter the postel Address:";
                                        getline(cin, st[i].addr.postelAddress);
                                    }
                                
                                
                                cout<<endl;
                                cout<<"Enter the roll number of the student that you want to get  the data: "<<endl;
                                getline(cin,n);
                                for(int i=0;i<4;i++){
                                   if(st[i].rollNo==n){
                                       cout<<"The name of the student = "<<st[i].name<<endl;
                                       cout<<"The rollNo of the student = "<<st[i].rollNo<<endl;
                                       cout<<"The age of the student = "<<st[i].age<<endl;
                                       cout<<"The gender of the student = "<<st[i].gender<<endl;
                                       cout<<"The department of the student = "<<st[i].department<<endl;
                                       cout<<"The semister of the student = "<<st[i].semister<<endl;
                                       cout<<"The hostel of the student = "<<st[i].hostel_no<<endl;
                                       cout<<"The room_no of the student = "<<st[i].room_no<<endl;
                                       cout<<"The country of the student = "<<st[i].addr.country<<endl;
                                       cout<<"The division of the student = "<<st[i].addr.division<<endl;
                                       cout<<"The district of the student = "<<st[i].addr.district<<endl;
                                       cout<<"The city of the student = "<<st[i].addr.city<<endl;
                                       cout<<"The postel address of the student = "<<st[i].addr.postelAddress<<endl;
                                   }
                                }
                            }
                        
                    

Whats make this website differents from others?

Amazing Feature That Increase your Productivity

If you don't find any problem,in this website and if you have a very difficult assignment question that is not present on internet ,then you dont bother yourself,we are here to solve every problem of your life you just click on the Problem asking button and write your problem we slove the code of your problem and sent you on your email addres that you put in the sin up form.