Thread: cin.getline mystery

  1. #1

    Question cin.getline mystery

    Does anyone know why you have to press enter twice when using cin.getline. e.g.

    const int MAX_BUFFER=255;

    void main()
    char Buffer[MAX_BUFFER];

    cout << "Please enter something: ";

    cout << "You entered " << Buffer;


  2. #2
    Registered User
    Join Date
    Nov 2001
    If you get rid of getchar() that won't be a problem.

  3. #3
    The ony reason why I am using getchar() is to hold the screen for the user. It is still neccesary for me to press the enter twice when getting data from the keyborad buffer; using cin.getline

  4. #4
    Registered User
    Join Date
    Nov 2001
    the getchar() at the bottom is causing the double enter

  5. #5
    With the getchar there i would have to press enter 3 times for the program to quit. So if I never put it in i would still have to press enter twice for the program to quit.

  6. #6
    As written I don't see a problem with your program. The call to cin.getline() work fine. If not please post your exact! program. There can be problems when using getiline(). The problem usually happens when mixing use of >> and getline() or one of the other istream methods and getline() in the same program. This problem stems from how the various istream methods deal with input. >> and other istream methods OTHER THAN getline() do not remove the terminating char from the istream buffer because they just ignore it. getline() doesn't ignore it however, and that can lead to problems.

    If you want to get detailed:
    User input is put into a buffer before it is stored in a variable or placed on the screen. When you use the >> operator to place a variable in the buffer it ignores any leading whitespace, (whitespace includes stuff like a space between words, a new line character, a tab character, and so on) and places all non white space input up to the next whitespace into the variable. It stops at the next whitespace and leaves the next white space intact. That can be very useful. For example if the user types in spaceBar spaceBar tab 123 enterKey, and the input is accepted by >>, the >> will ignore everything before 123, put 123 in the variable, and stop at the enterKey (which is really newline char to the computer) and leave the newline char in the buffer. If the user is then asked to input a single word, the newline char remaining in the buffer is ignored, and everything up to the next whitespace is entered into the variable.

    getline(), on the other hand, doesn't ignore leading white space. On the one hand this is good, because it allows you enter a phrase or a sentence or a whole novel at a time and store it all in the same variable. On the other hand if the first thing getline() sees in the buffer is a leftover newline char from a previous call to >> and if the terminating char for this call to getline() is the newline char (which it is by default, although you can change it if you wish), then getline() sees the first newline char and stops input before anything else can happen. It turns out that getline() also differs from other istream methods in that it disposes of the terminating char rather than leaving it in the buffer. Therefore if you were to place a second call to getline() right away the first call will have cleared the newline char from the buffer, the second call will see the input and away you go. However, this is not the recommended approach.

    The recommeded approach, or at least the one I've seen in multiple places, is to call the ignore() method of istream classes after ever call to >> IF you are going to be using getline() in the program and/or IF this is a piece of software that will be potentially modified by someone else in the future.

    char ch ;
    char buffer[80];

    cout << "enter a single character" << endl;
    cin >> ch;
    cout << "enter a phrase" << endl;
    cin.getline(buffer, 79);

    There are a bunch of other issues dealing with specifics. Do you ignore just 1 char or up to 80 char or untile the terminating char for ignore() is encountered, or up to 256 char our until the terminating char is identified, or what?

    cin.getline(buffer, 79, '~'); means accept up to 79 char and place them in the char[] called buffer unless you find a ~ first. If you find the ~ first discard the ~. Of course if buffer has been declared like this:

    char buffer[25];

    then the call to getline(() above will get you into trouble, too.

    Is it any wonder that C++ is felt to be a difficult language to learn??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  2. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM
  3. Mystery.
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 01:41 AM
  4. getche() with cin.getline() ?
    By marCplusplus in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2001, 02:13 AM