Do I need to use cin.get() after each use of cin?
That's the most naive way of dealing with the issue. The problem is that cin's >> operator may leave a newline character on the stream for getline (or the sibling get) to terminate immediately on. Ideally you would read all input as a full line and then parse accordingly. For example:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct car { char make[20]; int year; };
template <typename T>
bool get_input(istream& in, T& value)
{
string line;
return getline(in, line) && istringstream(line)>> value;
}
int main()
{
int cars;
cout << "How many cars do you want to catalog? ";
get_input<int>(cin, cars);
car *autos= new car[cars];
int i = 0;
while (i < cars)
{
cout << "Car # "<< i + 1 << endl;
cout << "Please enter the make: ";
cin.getline(autos[i].make, 20);
cout << "\nPlease enter the year: ";
get_input<int>(cin, autos[i].year);
i++;
}
cout << "Here is your collection: ";
int j = 0;
while (j < cars)
{
cout << autos[j].year << " " << autos[j].make <<endl;
j++;
}
delete [] autos;
cin.get();
cin.get();
return 0;
}