My loop skips over user input option

Hello. I'm going through C++ Primer Plus. The exercise question says:

Design a structure called car that holds the following information about an automobile:its make, as a string in a character array or in a string object, and the year it was built,as an integer. Write a program that asks the user how many cars to catalog. The program should then use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care because it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure.

Now I have this down for the most part but when running the program the loop skips over the input of the make of the vehicle and goes directly to the year input. It works when I use your standard cin >> to insert the info into the array however this of course doesn't work for multiple word input. With getline() or get(), whether using a string object or character array, the make input is skipped altogether. Any ideas? Thanks.

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
41
42
43
44
45
46
47
48
49
50
51
//Exercise6 -- collect car info

#include <iostream>
#include <string>

//A struct is created that stores info on make and year
struct cars
{
       std::string make;
       int year;
};

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    
    //Retrieve the number of cars to be cataloged
    cout << "How many cars do you wish to catalog? ";
    int numOfCars;
    cin >> numOfCars;
    
    //Dynamically allocate memory for the data to be stored
    cars * pCars = new cars[numOfCars]; 
    
    //Prompt user to enter the car info and and then
    //insert it into the array
    for (int i = 0; i < numOfCars; i++)
    {
         cout << "Car# " << i + 1 << endl;
         
         cout << "Please enter the make: ";
         //cin >> pCars[i].make;
         getline(cin,pCars[i].make);
         
         cout << "\nPlease enter the year: ";
         cin >> pCars[i].year;
         cout << endl << endl;   
    }
    
    //Display gathered info on screen
    cout << "Here is your collection: " << endl;
    
    for (int i = 0; i < numOfCars; i++)
        cout << pCars[i].year << " " << pCars[i].make
             << endl;
    
    delete [] pCars;
              
}
It's an issue of mixing both cin() and getline() - you can find many solutions in this forum if you look.
Read the entire section on iostreams at this FAQ site.
http://www.parashift.com/c++-faq-lite/input-output.html

Search the articles forum in case there are more but there are many articles on this. Another solution that would require some research is to overload operator>> and operator<< for the cars class. It isn't a POD struct anyway so you might as well make it a class and give it a constructor and some overloaded operators that will make it more functional. Also you aren't checking for any input errors and as bluezor indicates you are probably having issues with the '\n' being left in the input stream so you can fix this when you fix the input validation issue by using the ignore function on the input stream to discard the '\n' characters.

jsmith wrote something there near the bottom that might be useful to copy and paste. In general you could write your own template input function to do the error checking and stream input generically.
http://cplusplus.com/forum/articles/9741/
http://cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.