Page 1 of 1

Help with cin.get and eof

Posted: Thu Jan 15, 2009 2:44 am
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");
}  

Re: Help with cin.get and eof

Posted: Thu Jan 15, 2009 8:29 am
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
}  

Re: Help with cin.get and eof

Posted: Thu Jan 15, 2009 2:40 pm
by afaik
Thanks Marauder, I'm going to try tonight after classes to get it to work.

Re: Help with cin.get and eof

Posted: Fri Jan 16, 2009 2:01 am
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.

Re: Help with cin.get and eof

Posted: Fri Jan 16, 2009 2:47 am
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?

Re: Help with cin.get and eof

Posted: Fri Jan 16, 2009 2:00 pm
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.

Re: Help with cin.get and eof

Posted: Fri Jan 16, 2009 2:44 pm
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)).

Re: Help with cin.get and eof

Posted: Thu Jan 22, 2009 1:06 am
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 "

Re: Help with cin.get and eof

Posted: Thu Jan 22, 2009 8:37 am
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.

Re: Help with cin.get and eof

Posted: Thu Jan 22, 2009 9:53 pm
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.