Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

구조체에 학생 정보를 저장하는 C++ 프로그램

<시간/>

구조는 다양한 데이터 유형의 항목 모음입니다. 다른 데이터 유형 레코드를 사용하여 복잡한 데이터 구조를 생성하는 데 매우 유용합니다. 구조체는 struct 키워드로 정의됩니다.

구조의 예는 다음과 같습니다.

struct employee {
   int empID;
   char name[50];
   float salary;
};

학생 정보를 구조체로 저장하는 프로그램은 다음과 같습니다.

예시

#include <iostream>
using namespace std;
struct student {
   int rollNo;
   char name[50];
   float marks;
   char grade;
};
int main() {
   struct student s = { 12 , "Harry" , 90 , 'A' };
   cout<<"The student information is given as follows:"<<endl;
   cout<<endl;
   cout<<"Roll Number: "<<s.rollNo<<endl;
   cout<<"Name: "<<s.name<<endl;
   cout<<"Marks: "<<s.marks<<endl;
   cout<<"Grade: "<<s.grade<<endl;
   return 0;
}

출력

The student information is given as follows:
Roll Number: 12
Name: Harry
Marks: 90
Grade: A

위의 프로그램에서 구조체는 main() 함수보다 먼저 정의됩니다. 구조에는 학생의 롤 번호, 이름, 점수 및 학년이 포함됩니다. 이는 다음 코드 스니펫에 나와 있습니다.

struct student {
   int rollNo;
   char name[50];
   float marks;
   char grade;
};

main() 함수에서 struct student 유형의 객체가 정의됩니다. 여기에는 롤 번호, 이름, 마크 및 등급 값이 포함됩니다. 이것은 다음과 같이 표시됩니다.

struct student s = { 12 , "Harry" , 90 , 'A' };

구조 값은 다음과 같이 표시됩니다.

cout<<"The student information is given as follows:"<<endl;
cout<<endl;
cout<<"Roll Number: "<<s.rollNo<<endl;
cout<<"Name: "<<s.name<<endl;
cout<<"Marks: "<<s.marks<<endl;
cout<<"Grade: "<<s.grade<<endl;