I am new to C++ and working through SAMS C++ primer. one of the excercies involves creating a dynamic array of structures to catalogue car information. I have the following code almost working and I am sure that I am missing something really simple.
The program compiles and runs as expected, however on the first run through the loop, the "make" of the car is not stored in the array. All subsequent loops capture the input and display properly.
Any help or point in the right direction would be appreciated.
thanks.
//c++ primer ch 5 ex 6.
#include <iostream>
using namespace std;
struct car {
char make[20];
int year;
};
int c;
int t = 1;
int main() {
cout <<"How many cars would you like to catalogue? ";
cin >>c;
cin.get();
car * catalogue = new car[c];
car *ptr = &catalogue[0];
for (int i = 0;i < c; ++i)
{
cout <<"Car# "<<t<<":\n";
cout <<"Enter make of car: ";
cin.getline(ptr[i].make,20);
cout <<"Enter year of car: ";
cin >> ptr[i].year;
cin.get();
cout <<"\n";
++t;
};
delete [] catalogue;
cout <<"Your catalogue of cars: \n";
for (int i = 0; i < c;++i)
{ cout <<catalogue[i].year<<" "<<catalogue[i].make<<"\n";
};
}