Thread: How to recognize <Enter> with getline()?

  1. #1
    Unregistered
    Guest

    How to recognize <Enter> with getline()?

    I'd like to recognize that the enter key was hit when I use getline().

    For ex.

    string someString = "anything";
    while(someString != "")
    {
    cout << "Enter a word: ";
    getline(cin, someString, '\n')
    }

    I want the while loop to continue unless the user enters the enter key, and only the enter key (no characters or spaces before or after).

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    isn't that what getline does anyway??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    k right im with u now..... what is the purpose of that when you will keep overwriting the previous input?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Unregistered
    Guest
    Ok wait, I gave a terrible example. A loop in main is supposed to call two functions repeatedly. The first, GetWord(), is supposed to get a word from the user. If the user hit the enter key instead of typing a word, the loop should end. If a word was entered, that word should be saved and the loop should continue.

    If I use a cin >> statement instead of getline(), whenever I type enter as a word, the cursor moves down a line but still waits for a word.

    If anyone can suggest anything it would help alot because I'm losing it right now!

    Here's the loop in main():

    string mainsWord;
    // Continuous loop until user hits <Enter>
    do
    {
    // Call to get and capitalize word
    mainsWord = GetWord();

    // If the word is not null, save
    if(mainsWord != "")
    SaveWord(coutfile, mainsWord);

    }while (mainsWord != "");

    Here is GetWord():

    // Prompts user for word, sends to allcaps for complete capitalization, then returns the capitalized word by value

    string GetWord ()
    {
    string usersWord;

    // Prompt user for word, gather word
    cout << "Please enter a word (<Enter> key to stop): ";
    cin >> usersWord;


    // Send word to allcaps(), return fully captilized word
    return allcaps(usersWord);

    }

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could use strlen() on the returned returned word, and if it's zero exit the loop.
    zen

  6. #6
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43
    That should help you:


    while (GetStringFunction())
    {
    ... do something with it....
    }


    int GetStringFunction()
    {
    char name[100];
    cout << "enter something";
    cin.getline(name, 100);

    if (name[0] == '\0' ) // user just pressed Enter
    {
    return 0;
    }
    else
    {
    cout << "data was inputed!"
    return 1;
    }
    }
    C++ rulez!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM