Thread: detecting and of input

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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