How to read empty string from command line

This is a discussion on How to read empty string from command line within the C++ Programming forums, part of the General Programming Boards category; Hi I wanted to have my program behave in the following way: "Please press enter when you are ready." and ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    9

    How to read empty string from command line

    Hi I wanted to have my program behave in the following way:

    "Please press enter when you are ready."

    and then it would wait until you've pressed enter. But I've found no way of just reading a carriage return. I've hacked my around it by making the program expect something like an int i.e.

    int x = 0;

    cin >> x;

    Forcing the user to hit an arbitary number on the keypad and then press Enter.

    I've read up on getline() and cin.ignore() but I don't think those are what I'm looking for.

    Any help would be really appreciated. thanks!

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    If i remember right yu can try

    system("pause");

    or

    cin.getline();

    or

    getch();

    Last one requires the conio.h header

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Have you used just plain ol cin.get() this seems to work fine for me
    I personally encapsulate it in a function e.g.
    Code:
     
    void waitForEnter()
    {
        std::cout<<"Press Enter To Continue"<<std::endl;
        std::cin.get();
    }
    Last edited by prog-bman; 08-07-2004 at 03:13 PM.
    Woop?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by joeyzt
    system("pause");
    dont use this. it is about as overkill as you can possibly go...

    try: cin.get();

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I like to use getline(), since it cleans out the keyboard input buffer, including the '\n' (for the benefit of subsequent >>cin or getline() operations.

    Something like:

    Code:
    #include <iostream>
    
    int main()
    {
      void wait_for_enter(void);
      char name[BUFSIZ];
    
      std::cout << "Let's play a game: " << std::endl << std::endl;
      std::cout << "Press \"Enter\" when you are ready: ";
      wait_for_enter();
      std::cout << std::endl << "  OK! Ready to begin" << std::endl;
      std::cout << std::endl << "  Please enter your name: ";
      std::cin.getline(name, sizeof(name));
      std::cout << std::endl << "  Hi, " << name << std::endl;
     // etc.
     // etc.
      return 0;
    }
      
    void wait_for_enter(void)
    {
      char ch[BUFSIZ];
      std::cin.getline(ch, sizeof(ch));
    }

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    whups i meant cin.get();

    not cin.getline();

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    9

    Thanks..

    but for whatever reason my program does not wait for the user even with cin.get() or cin.getline().

    eg.

    Code:
    cout << "press enter to begin" << endl; 
    
    cin.get();
    
    cout << "blah blah blah";
    when this code is executed the program will print

    press enter to begin
    blah blah blah

    without waiting for a carriage return input from the user after the first line is printed.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>for whatever reason my program does not wait for the user even with cin.get() or cin.getline().
    Prior calls to cin using the >> operator will likely leave a newline in the input stream. This will cause both get and getline terminate immediately without waiting for input as you would expect. If you have no qualms against flushing the input stream then this will work just fine for you.
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Do not forget to include <limits> as it holds numeric_limits.

    A better solution of course is to eschew formatted input in favor of line input and a manual conversion.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    9

    Thanks

    that did the trick!

    thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21