3
#include <iostream>
#include <string>
using namespace std;

struct car
{
    string make;
    int year;
};

int main()
{
    int n;
    cin >> n;
    car * pt = new car[n];
    for (int i=0; i<n; i++)
    {
        getline(cin, pt[i].make);
        cin >> pt[i].year;
    }
    for (int i=0; i<n; i++)
        cout << pt[i].year << ' ' << pt[i].make << endl;
    return 0;
}

When I keyed in the input, I can only key in one number and one string. The program then displays some zeros. It prevents me from keying in more input. Can anybody explain me to what happened and how to solve this problem in C++? Thank you!

user1050165
  • 141
  • 1
  • 7
  • What is your code actually supposed to do? – IanPudney Jun 11 '13 at 18:51
  • 3
    Use `std::vector` and search "C++ getline skipping" to find tens of questions with the same problem. – chris Jun 11 '13 at 18:52
  • @quinxorin It's a very simple program which asks user to key in the number of cars that he wishes to catalog and key in the make and year made of those cars. Finally the program displays all the information collected. – user1050165 Jun 11 '13 at 18:53

1 Answers1

3

Try the following:

 for (int i=0; i<n; i++)
 {
    cin.clear();
    cin.ignore();
    getline(cin, pt[i].make);
    cin >> pt[i].year;  
 }

The reason is that after

cin >> pt[i].year;

When you input some "stuff" as year, you press enter. It will have \n left over in the cin stream. You need to have cin.ignore to ignore that \n character. You may find basic_istream/ignore useful.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • 1
    I think the problem lies between get line(cin, pt[i].make) and cin >> pt[i].year because I can only key in one string which is pt[0].make. After that, the program automatically displays multiple lines of zeroes and it terminates thereafter. – user1050165 Jun 11 '13 at 18:56
  • @user1050165 have you tried the suggested solution? Does it work for you? – taocp Jun 11 '13 at 18:57
  • New problem comes, here is the output:2 Hudson Hornet 1952 0 1952 udson Hornet – user1050165 Jun 11 '13 at 18:59
  • I'm only allowed to key in one more number which is the year. And the output is wrong. – user1050165 Jun 11 '13 at 19:00
  • @user1050165 It works fine for me. I input the following: 2 (n=2) Hoston hornet 2; String w 3; Output is the following: 2 Hoston hornet 3 String w – taocp Jun 11 '13 at 19:07
  • 1
    I got it correct. When cin.clear() and cin.ignore() are moved in front of the getline function, it works. But...could you explain what cin.clear() and cin.ignore() did in the program? – user1050165 Jun 11 '13 at 19:22
  • And...If I replace cin.ignore() by another cin.clear(), it doesn't work. – user1050165 Jun 11 '13 at 19:23
  • @user1050165 cin.ignore is already explained in the post. cin.clear will clear the error flag of input stream. You may look at cin.clear documentation for complete information? – taocp Jun 11 '13 at 19:23
  • 1
    @user1050165 take a look at this post? http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – taocp Jun 11 '13 at 19:24
  • Thanks a lot. But why putting cin.clear() and cin.ignore() after the getline and cin function doesn't work? – user1050165 Jun 11 '13 at 19:41
  • @user1050165 this is because when you enter value for n, there is also \n left over. – taocp Jun 11 '13 at 20:00