【发布时间】:2022-06-23 00:22:48
【问题描述】:
我制作了这个程序并将 c 和 c++ 混合在一起,任何人都可以帮助我用 c++ 制作这个程序。 专门把这个结构做成类。 //这只是一个评论,因为我无法发布这个问题,所以请忽略斜线之间的任何内容//
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
char ch,choice,disp;
int i=0,c=0;
cout<<"Do you want to\n1. Store details of students.\n2. Display the details of students."<<endl;
cin>>ch;
switch (ch)
{
case '1':
cout << "Enter information of students: " << endl;
// storing information
do{
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;
i++;
c++;
cout<<"Do you want to add one more entry ?(y/n)"<<endl;
cin>>choice;
}while(choice == 'y' || choice == 'Y');
cout<<"Want to display all the students info ?(y/n)";
cin>>disp;
if(disp=='n' || disp=='N')
{
break;
}
case '2':
cout << "Displaying Information: " << endl;
// Displaying information
for(int i = 0; i < c; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
}
break;
default:
cout<<"The page is in development please choose valid choices";
}
return 0;
}```
【问题讨论】:
-
C++ 中
struct和class的唯一区别是struct成员默认为public,而class成员默认为private。所以把struct改成class,在成员前加上public:。
标签: c++ c loops class structure