Working through C++ Primer Plus and am trying to cin data to a dynamically allocated array of structures. One of the items is a char array. How do I write to these struct members? Posting code of my wrong attempt so you can see what I'm trying to do.
#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!