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<<"学生信息如下:"<<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;
}输出结果
学生信息如下: Roll Number: 12 Name: Harry Marks: 90 Grade: A
在上述程序中,结构是在main()方法之前定义的。该结构包含学生的卷号,姓名,分数和等级。下面的代码段对此进行了演示。
struct student {
int rollNo;
char name[50];
float marks;
char grade;
};在main()函数中,定义了structstudent类型的对象。其中包含卷号,名称,标记和等级值。如下所示。
struct student s = { 12 , "Harry" , 90 , 'A' };结构值以以下方式显示。
cout<<"学生信息如下:"<<endl; cout<<endl; cout<<"Roll Number: "<<s.rollNo<<endl; cout<<"Name: "<<s.name<<endl; cout<<"Marks: "<<s.marks<<endl; cout<<"Grade: "<<s.grade<<endl;
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短