Using an Array of Structures in Simple Program

Hello everyone,

I'm very new to C++, so bear with me.

I'm trying to do some of my C++ homework at the moment and I've run into a problem. Essentially, the program is supposed to have a structure of type car that has a make and year. It's supposed to make a dynamic array of that structure and let the user input the make and year of each of the cars that they catalog. It then displays their "collection". Here is what the output is supposed to look like after a user has input their information:

How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser

The problem is that with my current code, if I try and run it, it neglects to give the user a chance to input anything, and I don't know why.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
//#include <string>
using namespace std;

int main()
{
	struct car
	{
		char make[20];
		int year;
	};

	int numCars;
	int x = 1;

	cout << "How many cars do you wish to catalog? ";
	cin >> numCars;
	cout << endl;
	car * pt = new car[numCars];

	for (int i = 0; i < numCars; i++)
	{
		x += i;
		cout << "Car #" << x << ":" << endl;
		cout << "Please enter the make: ";
		cin.get(pt[i].make, 20);
		cout << "Please enter the year made: ";
		cin >> pt[i].year;
		cout << endl;
	}

	cout << "Here is your collection: " << endl;

	for (int z = 0; z < numCars; z++)
	{
		cout << pt[z].year << " " << pt[z].make << endl;
	}

	return 0;
}


Also, sorry for the lack of comments. =/
It runs fine if you use cin >> pt[i].make; in line 26 instead of cin.get(pt[i].make, 20);

Return 0;
Oh yeah. I forgot to mention that the make has to be able to contain one or more words. That works fine with one word, but if you try more than that then it doesn't. :(
Any specific reason why you aren't using a string instead for char make[20];?
I've tried to do that, which is why //#include <string> is commented out.

When I do that, I get build errors and it says this:

Error 1 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *' e:\c++ programming i\cprog 221\chapter 5\chapter 5 - assignment 6\chapter 5 - assignment 6\chapter 5 - assignment 6.cpp Line: 30


Unless maybe I'm declaring it wrong or something:

string make;
Oh yeah. I forgot to mention that the make has to be able to contain one or more words. That works fine with one word, but if you try more than that then it doesn't. :(


http://www.cplusplus.com/forum/articles/6046/
I've tried that, except in the form of cin.getline(pt[i].make, 20);

When I do that, instead of skipping over all user input, it only skips over giving the user a chance to input the make.

And even if I try to do it the way the article explains it, I get the same problem.
Last edited on
You have to replace ALL instances of cin >> var. Using the >> operator will not remove the newline character from the stream. Thus cause your problems. >> also won't handle multiple worlds with spaces on a single line.
When I replaced all of the instances of >>, it gave me a TON of errors.

Also, I know that >> won't handle multiple words with spaces, that's why I used cin.getline().

I just don't understand why it does that when it works perfectly fine in the examples from the book and also in my other programs.
Read the article properly. Don't just try to copy the code.

Take you original code, and when it asks you to enter number of cars. Enter something like "alpha" and see what happens.
I know that it's going to break.

But that's not the problem. The chapter my class is on in the book has not explained if-then statements and such yet. The program that it wants me to write expects that the user will input the correct type of data, thus we will not encounter that kind of a problem.

The problem that I am experiencing is different than the one outlined in that article. For some reason my program will neglect to give the user a chance to enter any input about the information for the cars. Copy the code into Visual Studio and try it and you'll see what I'm talking about.

If I try to use cin.getline() it just neglects to let the user enter input about the make of the car, and lets them only enter the year that the car was made.
Yes. That's because as I said.
Using the >> operator will not remove the newline character from the stream.
.

So after you enter the numcars it keeps the newline character in the stream. After you prompt to enter the make, that newline is used to submit a blank make for the car. And the cycle continues after you enter the year.

After each line of accepting input via cin >> var; you will need to add std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); for cin to ignore all newline characters. This still won't fix the multiple word problem though.
Topic archived. No new replies allowed.