C++ C++;指针(student*[n]给出数组类型不可赋值的错误) #包括 #包括“student.h” 使用名称空间std; int main() { //输入学生人数 int n; cout n; 学生*s[n]; 串tmp; 双t; //输入每个学生的详细信息 对于(int i=0;i
您应该使用likeC++ C++;指针(student*[n]给出数组类型不可赋值的错误) #包括 #包括“student.h” 使用名称空间std; int main() { //输入学生人数 int n; cout n; 学生*s[n]; 串tmp; 双t; //输入每个学生的详细信息 对于(int i=0;i,c++,C++,您应该使用likestd::vector s(n); 也可以在代码的开头添加#包括,以使用它。最好使用std::vector,并完全避免使用new/delete。 #include <iostream> #include "student.h" using namespace std; int main() { // inputting the number of students int n; cout << "
std::vector s(n);
也可以在代码的开头添加
#包括,以使用它。最好使用std::vector
,并完全避免使用new
/delete
。
#include <iostream>
#include "student.h"
using namespace std;
int main()
{
// inputting the number of students
int n;
cout << "How many students would you like to process?" << endl;
cin >> n;
student* s[n];
string tmp;
double t;
// entering each student details
for (int i = 0; i < n; i++)
{
// dynamically allocating object
s[i] = new student();
cout << "Enter first name for student " << (i + 1) << endl;
cin >> tmp;
s[i]->setFirstName(tmp);
cout << "Enter middle name for student " << (i + 1) << endl;
cin >> tmp;
s[i]->setMiddleName(tmp);
cout << "Enter last name for student " << (i + 1) << endl;
cin >> tmp;
s[i]->setLastName(tmp);
cout << "Enter GPA for student " << (i + 1) << endl;
cin >> t;
s[i]->setGPA(t);
}
double avgGPA = 0;
// printing the student details
cout << "Students:" << endl;
cout << "---------" << endl
<< endl;
for (int i = 0; i < n; i++)
{
cout << s[i]->getFirstName() << " " << s[i]->getMiddleName() << " " << s[i]->getLastName() << " " << s[i]->getGPA() << endl;
avgGPA += s[i]->getGPA();
}
avgGPA /= n;
// printing the average GPA
cout << endl
<< "Average GPA: " << avgGPA;
// freeing the memory allocated to objects
for (int i = 0; i < n; i++)
delete s[i];
return 0;
}