Help with cin.get and eof

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Help with cin.get and eof

Post by afaik »

What I'm doing is very basic, but it's to familiarize myself with basic input/output. I'm using dev-c++ and when I input characters into the program below, for example 'asdf', it loops fine. If I enter 'asdf(ctrl-z)' for end of file, program will output the asdf, but wont display cout << "eof reached" or exit the loop. If I enter ctrl-z by itself, program will output cout << "eof reached" and exit the loop. Can anyone point out what I'm doing wrong or where my logic is wrong.

Code: Select all

#include <iostream>
using namespace std;

int main() {

char temp;
char nwln = '\n';

cout << "EOF: " << EOF << endl;
cout << "Input something: ";

do
{
  cin.get(temp);  // temp = cin.get();  //  cin >> temp;

  while(temp != nwln)
    {  
    if(temp != '\n' && temp != cin.eof() )  
      cout.put(temp) ; 
        
    cin.get(temp); 
    }

  if(temp == '\n' )
    //cout << "New Line entered" << endl;
    cout << "/n" << endl;

  if(cin.eof() )
    cout << "END OF FILE REACHED";

cout << "\nEOF: " << cin.eof() << endl;

}while(!cin.eof() ); 

cout << "\n\n" ; 
system("PAUSE");
}  
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with cin.get and eof

Post by MarauderIIC »

Code: Select all

#include <iostream>
using namespace std;

int main() {

char temp;
const char NEWLN = '\n'; //style correction, also since newln isn't ever assigned to, make it const

cout << "EOF: " << EOF << endl;
cout << "Input something: ";

do
{
  cin.get(temp);

//without the condition change here, with an input of 'asdf^Z\n', eof won't be "found" for your main loop,
//even though *this* loop will exit, because the prev version of this loop ignores eof and will advance cin past it
  while(temp != NEWLN && !cin.eof()) 
    {  
    if(temp != NEWLN && temp != cin.eof() )  
      cout.put(temp) ; 
        
    cin.get(temp); 
    }

  if(temp == NEWLN )
    //cout << "New Line entered" << endl;
    cout << "/n" << endl;

  if(cin.eof() )
    cout << "END OF FILE REACHED";

cout << "\nEOF: " << cin.eof() << endl;

}while(!cin.eof() ); 

cout << "\n\n" ; 
system("PAUSE");
}  
I think that'll work ^.

Although a better way to do it would be

Code: Select all

#include <iostream>
using namespace std;

int main() {

char temp;
const char NEWLN = '\n'; //style correction, also since newln isn't ever assigned to, make it const

cout << "EOF: " << EOF << endl;
cout << "Input something: ";

cin.get(temp) //priming read
while (cin) //this detects EOF as well as stream read errors!
{

  if(temp == NEWLN )
    cout << endl; //endl prints a newline, as well as forcing the output to be written to screen. unless you really wanted to doublespace?
  else if(cin.eof() )
    cout << "END OF FILE REACHED";
  else //must be a char we want to put back
    cin.put(temp);

cout << "\nEOF: " << cin.eof() << endl;

  cin.get(temp);
}

cout << "\n\n" ; 
system("PAUSE");
return 0; //always put a return value on main so you're in control of what you return to OS on success
}  
Last edited by MarauderIIC on Thu Jan 15, 2009 8:31 am, edited 1 time in total.
Reason: see sig
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: Help with cin.get and eof

Post by afaik »

Thanks Marauder, I'm going to try tonight after classes to get it to work.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: Help with cin.get and eof

Post by afaik »

Well, I spent some time with it tonight but haven't made much progress. The only realization is that once the program gets to ctrl-z after going through lets say 'asdf(ctrl-z)', it doesn't accept ctrl-z, as input.

If I do ctrl-z as the only input, it will bypass while(cin) altogether.

So unless I'm wrong, ctrl-z (EOF) isn't accepted by cin, . Sigh, I've been working on this for a couple days on and off trying to solve this alone and haven't made progress. I'll see if I can ask around, but if anyone here knows how to handle this off the top of their heads, it would save me a couple more hours.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help with cin.get and eof

Post by Ginto8 »

First, why are you doing "end of file"? wouldn't you be working with file i/o then?

Second, You don't have to use the eof character. You could use another one that isn't used much, like '~' or '`'. Here's what I would do:

Code: Select all

#include <iostream>
#include <conio.h> // for getch()
using namespace std;

int main() {

   char temp;
   const char NWLN = '\n';
   const char EOF = '~'

   cout << "EOF: " << EOF << endl;
   cout << "Input something: ";

   do
   {
     temp = getch();  // temp = cin.get();  //  cin >> temp;

     while(temp != nwln)
     {  
       if(temp != '\n' && temp != cin.eof() )  
          cout.put(temp) ; 
        
       temp = getch(); 
     }

     if(temp == '\n' )
     //cout << "New Line entered" << endl;
     cout << "/n" << endl;

     if( temp == EOF )
        cout << "END OF FILE REACHED";

        cout << "\nEOF: " << EOF << endl;

   }while( temp != EOF ); 

   cout << "\n\n" ; 
   getch();
}
By the way, why didn't you do something easy like a calculator application?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: Help with cin.get and eof

Post by afaik »

Ginto8 wrote:First, why are you doing "end of file"? wouldn't you be working with file i/o then?
Well, I was hoping to get to working with file input/output next, so this is to familiarize myself a little bit with that before I actually get to it.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with cin.get and eof

Post by MarauderIIC »

EOF is reached when EOF is reached. I wasn't sure about cin taking EOF either, myself (since there's not necessarily any END to input). File I/O you use essentially the structure I gave you (priming read, while (file)).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: Help with cin.get and eof

Post by afaik »

Unbelievable...After playing around with this for two weeks and learning NOTHING, I've found some info that might help anyone else having problems with EOF on a windows machine.

Bjarne Stroustrup:
The program ends reading input when it sees "end of file". If you run the program from the keybord on a Unix machine "end of file" is Ctrl-D. If you are on a Windows machine that because of a bug doesn't recognize an end-of-file character...
http://www.research.att.com/~bs/bs_faq2.html#simple-program
(Scroll to last bullet of that part)

Google Groups:
http://groups.google.com/group/comp.pro ... 42a91082cc
"Hello!
I have a problem with a C++ program under Windows but which works fine
under Linux. The program simply reads in integers from a file (source
code below)...
In Linux it reads all the numbers in the file no matter how EOF is
positioned: right after the last number or on the next line in
in_file. In Windows it won't read the last number unless there's an
empty line at the end of the file.
Example:
1 2 3 4 5 6 7EOF -> this works under Linux but not under Windows
1 2 3 4 5 6 7<CR>
EOF -> this works under both OS's "
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with cin.get and eof

Post by MarauderIIC »

I see. Thanks for posting your solution :) Sorry it gave you so much trouble (and we weren't much help). TBH I've never needed it to read to an eof character, I just read until there's nothing else to read in the file, as the loop I posted works just fine. For user input, I've simply used '\n' as a delimiter for getline() and ' ' as the delimiter for cin, in other words, the defaults.

Also, I'm not sure that cin.eof() detects if input failed in the way I would think of it (unlike what his comment says) -- such as inputting a char for an int. "!cin" would, though. It's shorthand for either !cin.good() or cin.bad(), I forget which.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: Help with cin.get and eof

Post by afaik »

Even though I stumbled across what I think was the reason for my problems, I took your code and played with it for a while. Thanks for your help and hopefully one day, sooner than later, I can help others with their own problems.
Post Reply