detecting and of input

This is a discussion on detecting and of input within the C++ Programming forums, part of the General Programming Boards category; I am wondering if there is any way to detect the end of a user's input in a console app ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    detecting and of input

    I am wondering if there is any way to detect the end of a user's input in a console app without having to use a sentinel character. Will someone please help me?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well in C, its
    Code:
    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        // do stuff
    }
    I imagine that the cin.getline() or whatever functions return some useful status to indicate end of file.

    You normally signal EOF on stdin by typing either ctrl-z or ctrl-d (depending on your OS)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I imagine that the cin.getline() or whatever functions return some useful status to indicate end of file.
    Yep, istreams have an implicit conversion that allows simple status checking:
    Code:
    while ( cin.getline ( buff, sizeof buff ) ) {
      // Do stuff
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Perhaps he wants to know if he read the whole line, inwhich case he needs to check to make sure the last character is '\n'. If you use a loop it will keep on begging for input until they give EOF (or an error occures) which is usually not what someone desires.

  5. #5
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    thanks a lot orbitz

Popular pages Recent additions subscribe to a feed

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