|
-
Dec 31st, 2002, 02:36 PM
#1
Thread Starter
Frenzied Member
!= -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.
-
Dec 31st, 2002, 03:22 PM
#2
Guru
EOF is very C-like...
I'd use something like:
Code:
while(!cin.eof()) {
// ...
letter = cin.get();
}
-
Dec 31st, 2002, 03:34 PM
#3
Thread Starter
Frenzied Member
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.
-
Dec 31st, 2002, 11:33 PM
#4
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.
-
Jan 1st, 2003, 09:30 AM
#5
Thread Starter
Frenzied Member
So I probably should not use eof but a specified # or character set that I choose.
-
Jan 1st, 2003, 11:31 AM
#6
Monday Morning Lunatic
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
-
Jan 2nd, 2003, 10:13 AM
#7
Thread Starter
Frenzied Member
I am just learning C++ and I have no idea what you mean by that. I am sorry.
-
Jan 2nd, 2003, 10:18 AM
#8
Addicted Member
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
-
Jan 2nd, 2003, 10:45 AM
#9
Thread Starter
Frenzied Member
How is the user going to know what to press to end the file? Does stream.eof() reveal it to them?
-
Jan 2nd, 2003, 10:49 AM
#10
Monday Morning Lunatic
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
-
Jan 2nd, 2003, 11:08 AM
#11
Thread Starter
Frenzied Member
Could you give a very small example of how stream.eof() is used and what the user will see?
-
Jan 2nd, 2003, 11:40 AM
#12
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.
-
Jan 2nd, 2003, 11:55 AM
#13
Guru
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.
-
Jan 2nd, 2003, 06:41 PM
#14
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.
-
Jan 2nd, 2003, 06:55 PM
#15
Monday Morning Lunatic
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
-
Jan 2nd, 2003, 09:04 PM
#16
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.
-
Jan 2nd, 2003, 10:34 PM
#17
Thread Starter
Frenzied Member
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.
-
Jan 3rd, 2003, 04:31 AM
#18
Monday Morning Lunatic
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
-
Jan 3rd, 2003, 09:33 AM
#19
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.
-
Jan 3rd, 2003, 09:57 AM
#20
Thread Starter
Frenzied Member
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?
-
Jan 3rd, 2003, 10:27 AM
#21
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.
-
Jan 5th, 2003, 01:11 AM
#22
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|