0

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;
}
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Narue
Featured Replies
  • 1
    Narue 5,707   5 Years Ago

    [QUOTE]Do I need to use cin.get() after each use of cin?[/QUOTE] 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 … Read More

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?

Edited by osirus0830: n/a

1

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;
}
Comments
Thanks so much
0

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.

0

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. ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.