The program is compiling and I'm not getting any errors, but when I run the program, after I enter the first value, the program just terminates.

//carstruct.cpp -- Structure with information about cars
#include <iostream>
using namespace std;
struct car {char make[20]; int year;};

int main()
{
	
	int cars;
	cout << "How many cars do you want to catalog? ";
	cin >> cars;
	car *autos= new car[cars];
	int i = 0;
	while (i < cars)
	{
		cout << "Car # "<< i + 1 << endl;
		cout << "Please enter the make: ";
		cin.get(autos[i].make, 20);
		cout << "\nPlease enter the year: ";
		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;
}

After experimenting with some things, I think it is something with cin. Do I need to use cin.get() after each use of cin?

When I enter the code like this, the program works.

//carstruct.cpp -- Structure with information about cars
#include <iostream>
using namespace std;
struct car {char make[20]; int year;};

int main()
{
	
	int cars;
	cout << "How many cars do you want to catalog? ";
	cin >> cars;
        cin.get();
	car *autos= new car[cars];
	int i = 0;
	while (i < cars)
	{
		cout << "Car # "<< i + 1 << endl;
		cout << "Please enter the make: ";
		cin.get(autos[i].make, 20);
		cout << "\nPlease enter the year: ";
		cin >> autos[i].year;
                cin.get();
		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;
}

My question is why do I have to add the extra cin.get()s?

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;
}
commented: Thanks so much +1

narue, thanks for the reply. I think the reason that I don't know how to do that is that I haven't gotten that far in my book yet. I'm using C++ Primer Plus, and I'm only on chapter 5 and I have never seen that stuff with template yet, so I think that is why the author just uses the cin.get() since he will probably cover the more advanced properties later.

Using cin.get() is fine when starting out. In fact, it helps one to learn the weaknesses of stream I/O and fully appreciate the recommended guidelines down the road. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.