getline(cin, string)

Discussion in 'C Programming' started by goofolution, Nov 6, 2006.

  1. goofolution

    goofolution

    Joined:
    Nov 6, 2006
    Messages:
    5
    Hey all i've almost completed a fairly large project but am having some strange problems within one of my functions using getline(). After entering the first string the program skips over the rest of the input and prompts for supplier again.

    Here's the function:

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

    int ConstantVariables(ShippingDetails &details)
    {
    while(1)
    {
    /* prompt user for input */
    cout << " Supplier: ";
    getline(cin, details.supplier);

    cout << endl << " Date: ";
    getline(cin, details.date);

    char option;
    cout << endl << " Are the values you entered correct?" << endl << endl;
    cout << " <y/n> ";
    cin >> option;
    if(option == 'y' || option == 'Y')
    break;
    }
    return 1;
    }

    The struct details contains strings supplier and date, along with some other which i have omitted as they do not cause the problem.

    any help would eb appreciated.
    goofolution, Nov 6, 2006
    #1
    1. Advertising

  2. goofolution

    goofolution

    Joined:
    Nov 6, 2006
    Messages:
    5
    ok so i checked around a bit more and the obvious solutoin is to add a getchar() at the beginning of the while loop, however i dont' like the fact that after entering the first string the cursor waits for another key stroke before moving to the next input. Is there any way to remove this?

    eg.

    supplier: someone
    _ <--- program hangs here
    goofolution, Nov 8, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Poof
    Replies:
    1
    Views:
    372
  2. Fred H
    Replies:
    6
    Views:
    7,384
    Kevin Goodsell
    Jan 23, 2004
  3. Simon Gibson

    cin.getline problems in case

    Simon Gibson, Feb 26, 2004, in forum: C++
    Replies:
    6
    Views:
    580
    Simon Gibson
    Feb 26, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,655
    Alex Vinokur
    Mar 6, 2005
  5. PulsarSL@gmail.com
    Replies:
    4
    Views:
    712
    PulsarSL@gmail.com
    Mar 29, 2007
Loading...

Share This Page