原文: https://www.programiz.com/cpp-programming/examples/information-structure-array
该程序使用结构存储 10 名学生的信息(姓名,名次和分数)。
要理解此示例,您应该了解以下 C++ 编程主题:
在该程序中,创建了一个结构student。
该结构具有三个成员:name(字符串),roll(整数)和marks(浮点)。
然后,我们创建了一个大小为 10 的结构数组,以存储 10 个学生的信息。
通过 C++ for循环,程序将从用户那里获取 10 名学生的信息,并将其显示在屏幕上。
示例:将信息存储在结构中并显示
#include <iostream>using namespace std;struct student{char name[50];int roll;float marks;} s[10];int main(){cout << "Enter information of students: " << endl;// storing informationfor(int i = 0; i < 10; ++i){s[i].roll = i+1;cout << "For roll number" << s[i].roll << "," << endl;cout << "Enter name: ";cin >> s[i].name;cout << "Enter marks: ";cin >> s[i].marks;cout << endl;}cout << "Displaying Information: " << endl;// Displaying informationfor(int i = 0; i < 10; ++i){cout << "\nRoll number: " << i+1 << endl;cout << "Name: " << s[i].name << endl;cout << "Marks: " << s[i].marks << endl;}return 0;}
输出
Enter information of students:For roll number1,Enter name: TomEnter marks: 98For roll number2,Enter name: JerryEnter marks: 89...Displaying Information:Roll number: 1Name: TomMarks: 98...
