我正在学习C++ Primer Plus,尝试从动态分配的结构体数组中输入数据。其中一个项目是char数组。我该如何写入这些结构体成员?以下是我错误尝试的代码,以便您了解我想做什么。
#include <iostream>
using namespace std;
struct contributions
{
char name[20];
double dollars;
};
int donors;
int main()
{
cout << "How many contributors will there be?\n";
cin >> donors;
contributions * ptr = new contributions[donors];
for(int i = 0; i <= donors; i++)
{
cout << "Enter donor name #" << i+1 << ": \n";
cin >> ptr->contributions[i].name;
cout << "Enter donation amount: \n";
cin >> ptr->contributions[i].dollars;
}
Thanks in advance!