我正在尝试创建一个学生结构,然后在该结构内部有一系列成绩(或标记),基于用户输入的标记。
然后我尝试创建Student结构的动态数组。
我想与这些指针互动,即输入学生信息和成绩然后cout他们然而我不认为我正在这样做。这是我的代码开头的一部分。我的主要问题是创建一个标记数组,我无法在我的教科书中找到如何声明它。
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
struct Student
{
string name;
int id;
int* mark;
~Student()
{
delete [] mark;
mark = NULL;
};
};
void initStudent(Student* ptr, int markNum, int studentNum ); // function prototype for initialization
void sayStudent(Student* ptr, int markNum, int studentNum); // function prototype for printing
//*********************** Main Function ************************//
int main ()
{
int marks, studentNum;
Student stu; // instantiating an STUDENT object
Student* stuPtr = &stu; // defining a pointer for the object
cout << "How many marks are there? ";
cin >> marks;
cout << "How many students are there?";
cin >> studentNum;
Student* mark = new int[marks];
Student* students = new Student[studentNum];
initStudent(students,marks,studentNum); // initializing the object
sayStudent(students,marks,studentNum); // printing the object
delete [] students;
return 0;
} // end main
//-----------------Start of functions----------------------------//
void initStudent(Student* ptr, int markNum, int studentNum)
{
for (int i = 0; i < studentNum; i++)
{
cout << "Enter Student " << i+1 << " Name :";
cin >> ptr[i].name;
cout << "Enter Student ID Number :";
cin >> ptr[i].id;
for (int j = 0; j < markNum; j++)
{
cout << "Please enter a mark :";
cin >> ptr[i].mark[j];
}
}
}
答案 0 :(得分:0)
您需要在每个postgres
元素中分配marks
数组。
Student
您也可以在Student* students = new Student[studentNum];
for (int i = 0; i < studentNum; i++) {
students[i].mark = new int[marks];
}
。
initStudent