I'm learning C++ from the book C++ Primer Plus. At the end of the chapter on loops, they want us to design a structure, allocate adequate memory for an array of such structures using new, then feed input data to it. I got this code:
#include <iostream>
using namespace std;
struct car{
char make[20];
int yearMade;
};
int main()
{
int count = 0;
cout<<"How many cars do you wish to catalog today? ";
cin>>count;
car * autocar = new car[count];
for(int i=1; i<=count; i++){
cout<<"Car #"<<count<<": "<<endl;
cout<<"Please enter the make: ";
cin.get(autocar[i-1]->make).get();
cout<<"Please enter the year manufactured: ";
cin>>autocar[i-1]->yearMade;
cout<<endl;
}
cout<<endl<<"Here is your collection: "<<endl;
for(int i = 1; i<=count; i++){
cout<<autocar[i-1]->yearMade<<" "<<autocar[i-1]->make<<endl;
}
delete [] autocar;
return 0;
}
Now, the error comes on line 21, 24 and 29. The compiler is complaining that the "base operand of '->' has non-pointer type 'car'".
Beats me. I made sure that the type is a pointer, as you can see in the statement in line 16. Any suggestions?