Results 1 to 22 of 22

Thread: != -1 exit while loop?

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    != -1 exit while loop?

    while ( (letter= cin.get () ) != -1)

    Would that work correctly? What I mean is the != -1 part used to exit the while loop.

    I am reading this college book on C++ and they used EOF instead of -1. Which one is better? End of File just seemed harder to tell what to press to me.
    Last edited by aewarnick; Dec 31st, 2002 at 03:15 PM.

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    EOF is very C-like...
    I'd use something like:
    Code:
    while(!cin.eof()) {
        // ...
        letter = cin.get();
    }

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Can I use -1 instead of eof?
    What is the advantage of using eof instead of -1?
    And, does it matter is eof is uppercase or not?
    And, I don't understand what eof will do because everything I try to compile in dev C++ gives error messages and doesn't run. So I can't test things out yet.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    EOF is independent of the character encoding. You don't have any guarantees that EOF will be -1 on every platform.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    So I probably should not use eof but a specified # or character set that I choose.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you want to read to the end of the file, check for stream.eof(), as was said before
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I am just learning C++ and I have no idea what you mean by that. I am sorry.

  8. #8
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    What is meant is that if you want to read a file all the way to the end (End Of File), then you should test for end of file - not a value. As said by CB, if you test for a value, it might not be the same on every platform, machine etc. etc. By testing for eof: stream.eof(), you are actually testing for the eof.

    Am I right? Or have I got the wrong end?

    HD

  9. #9

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    How is the user going to know what to press to end the file? Does stream.eof() reveal it to them?

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No. The file ends when it ends, i.e. no more data in it. If you want to signal EOF to an input stream, it's dependent on the OS/shell you're using. It's Ctrl-D for Unix on i386, and Ctrl-Z for DOS (I think).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Could you give a very small example of how stream.eof() is used and what the user will see?

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    bool basic_ios<Elem, Traits>::eof();

    Returns true if the current stream has reached the end of the file and characters can no longer be read from it.
    "Has reached the end of the file" is achieved in two ways: in text mode the EOF character (forgot the ASCII code), in binary mode the true end of the file as denoted by the file descriptor.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by parksie
    No. The file ends when it ends, i.e. no more data in it. If you want to signal EOF to an input stream, it's dependent on the OS/shell you're using. It's Ctrl-D for Unix on i386, and Ctrl-Z for DOS (I think).
    Ctrl-Z for all Microsoft consoles.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I keep pressing Ctrl+Z in UNIX too, suspending cat.
    Then I always get confused when I can't log out...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I use Ctrl-C to terminate things like cat on output, Ctrl-D to terminate input or logout.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    cat > file
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I still don't see the point of even using eof. I think it would be easier to use -1 or some letter combination to exit a loop.

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Trust me, the stream knows more about its internals than you do, and it's portable.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But if you expect the user to input something on the console then of course you can choose whatever key combination you like. But NOT -1. -1 is no key combination.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Ok, say that I did use eof. Would the user always be notiifed of what to push? Or would that just be something that is learned?

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    He wouldn't know unless he knows his OS. The average today's windows user doesn't even know what EOF is, much less how to force it in console mode.

    It depends on your input. If you need to read positive numbers from the user let him finish by entering a negative number. If you read any number let him finish by entering any character. If you read a long string where any character can occur, you're screwed But you shouldn't expect users to write an essay on the console anyway. You could for example stop if the user enters an unlikely sequence, like ==&==.
    If you read in a number of records which each consists of a few numbers and strings, give the user a choice between continuing and stopping between each record.

    And so on. Checking for eof on console input is not a good idea. Choose a stopping method that makes sense in your context and inform the user how to use it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    That is exactly the reply I was hoping for and expecting. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width