Problems with getline (cin, <string>)

Discussion in 'Mac Programming' started by maatlof, Dec 17, 2010.

  1. maatlof macrumors newbie

    Dec 15, 2010

    I've done this structure:

    struct info {
    string singer;
    string title;
    int stock;
    float price;

    and this function:

    void lectura (info &cd){
    cout << endl << "Introduce the CD's information: " << endl;
    cout << "Singer: ";
    getline(cin, cd.singer);
    cout << "Title: ";
    getline(cin, cd.titulo);
    cout << "Products in stock: ";
    cin >> cd.stock;
    cout << "Price: ";
    cin >> cd.price;

    It does run in the main function, where there is a loop that make it repeats. The first time it works ok, but the next times when it is supposed to get the singer's name "getline (cin, cd.singer)" it doesn't let me write it, because it appears the new "cout" "Title: " and it gets no string in it. The executable shows like this:

    Introduce the CD's information:
    Singer: Title: Hello
    Products in stock: 3
    Price: 2

    Why does it happen?

    Thank you very much!
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset

Share This Page