EOF and cin

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2615 Views - Last Post: 20 January 2011 - 08:27 AM Rate Topic: ***** 2 Votes

#1 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

EOF and cin

Posted 19 January 2011 - 02:09 PM

View Postn8wxs, on 19 January 2011 - 01:23 PM, said:

r.stiltskin correctly pointed out you need to reset the error flag set by the EOF character entered to end the numeric input. But the EOF character is still pending in the input buffer. So when you check the user's input to see if the user wants to loop, the EOF character is extracted and the test fails:

I'm puzzled by this post by n8wxs from this thread.

The OP's code seemed to work correctly after I added cin.clear(), and adding cin.sync() after that didn't seem to make any difference in Linux or in Windows.


I tried the following code under both Linux and XP:
#include <iostream>
using namespace std;

int main()
{
  char c=127;
  cin >> c;
  cout << (unsigned short)c << endl;
  cin.clear();
  cin >> c;
  cout << (unsigned short)c << endl;
  return 0;
}


The only difference that I can see is that in Linux when I enter Ctrl-D the default value of 127 is immediately printed and the program then waits for the next keyboard input. Whereas in Windows when I enter Ctrl-z, ^Z is echoed, then the program waits for <enter> before it actually reads the EOF and prints 127, and then waits for the next input.

So, in Windows my input is Ctrl-Z <enter> y <enter> and in Linux Ctrl-D y <enter>
and the output in Windows looks like this:
^Z
127
y
121

and in Linux it looks like
127
y
121

Either way, I don't see any 'EOF' character that needs to be extracted. Also, adding cin.sync() after the cin.clear() in this program doesn't seem to make any difference in its behavior in Linux or Windows. So what is it's purpose (and what was n8wxs referring to)?


(Edit: corrected typo in the Linux output.)

This post has been edited by r.stiltskin: 19 January 2011 - 02:18 PM


Is This A Good Question/Topic? 2
  • +

Replies To: EOF and cin

#2 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 02:12 PM

What IDE are you using in windows?
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: EOF and cin

Posted 19 January 2011 - 02:16 PM

View PostI_cant_C, on 19 January 2011 - 05:12 PM, said:

What IDE are you using in windows?


I tried running it inside CodeBlocks, and I also ran the executable directly in a command window (no IDE). Why should the IDE matter?
Was This Post Helpful? 0
  • +
  • -

#4 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 02:21 PM

I ran the code in codeblocks(10.05) and it didn't work until I added the cout.sync() after the cout.clear()... I was thinking maybe some compilers might automatically sync after a clear or something *shrugs* *sctraches head* oh well, I'm sure someone with deep knowledge might be able to answer this riddle ;)
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: EOF and cin

Posted 19 January 2011 - 02:26 PM

What do you mean by "it didn't work"? What didn't work?

Suppose you enter:
I_cant_C <enter> 80 90 <enter> 78 <enter> Ctrl-Z <enter>

Does that not get you to the "Option" prompt (to enter another student)?

(Have to go out for a while. I'll check back later.)

This post has been edited by r.stiltskin: 19 January 2011 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 02:34 PM

With just the cout.clear(); the program doesn't wait for the answer Y/N for entering another students data, it just outputs 'option:(results)' then continues to terminate the program <- i.e. doesn't work, as to why it works for you and not me is a mystery to me since you say the program runs on your code::blocks.

EDIT: I mean cin. (i'm tired lol)
2nd EDIT: code works if I use ^Z directly, but not when I type "end of file"

This post has been edited by I_cant_C: 19 January 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: EOF and cin

Posted 19 January 2011 - 02:58 PM

Another data point, Mac OS X:

$ ./stilts 
127
127
$


Adding cin.sync():
#include <iostream>
using namespace std;

int main()
{
    char c=127;
    cin >> c;
    cout << (unsigned short)c << endl;
    cin.clear();
    cin.sync();
    cin >> c;
    cout << (unsigned short)c << endl;
    return 0;
}



made no difference in the behavior.
Was This Post Helpful? 0
  • +
  • -

#8 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 03:17 PM

The origional program to which this problem arose pertained to a student grade system, after inputting a students grades you were to type 'end of file', it was adapted by the OP to allow to add more than 1 student.

I tried to use just cin.clear(), but it still didn't work to specs, after adding cin.sync(), it worked fine...

I've since tried the same program with just cin.clear and used the keyboard ^Z and this worked also, but the origional problem needed a more literal termination than to require the user to use ctrl+z, I think some confusion as to the origional problem has crept in somewhere, lol. :)

EDIT: spelling correction... >.>

This post has been edited by I_cant_C: 19 January 2011 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: EOF and cin

Posted 19 January 2011 - 03:31 PM

View PostI_cant_C, on 19 January 2011 - 03:17 PM, said:

The origional program to which this problem arose pertained to a student grade system, after inputting a students grades you were to type 'end of file', it was adapted by the OP to allow to add more than 1 student.

I tried to use just cin.clear(), but it still didn't work to specs, after adding cin.sync(), it worked fine...

I've since tried the same program with just cin.clear and used the keyboard ^Z and this worked also, but the origional problem needed a more literal termination than to require the user to use ctrl+z, I think some confusion as to the origional problem has crept in somewhere, lol. :)

EDIT: spelling correction... >.>

You're supposed to ctrl+z to terminate the while loop that reads homework grades :)
Was This Post Helpful? 0
  • +
  • -

#10 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 03:35 PM

oh, I know that works, but I was following the actual program that asked me to type 'end of file' when I finished entering the student homework grades... sorry if I was being a bit thorough, but I prefer to make sure someone can follow the instructions a program gives on-screen rather than rely on their knowledge of computer languages little quirks :)

EDIT: in real world I'd expect a prog like that to end-up in the hands of a teacher, and even if their teaching c++, I'd not expect them to know keyb shortcuts or esc sequences (seeing how incompetent they seem to be) ;)

This post has been edited by I_cant_C: 19 January 2011 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#11 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: EOF and cin

Posted 19 January 2011 - 03:44 PM

View PostI_cant_C, on 19 January 2011 - 03:35 PM, said:

oh, I know that works, but I was following the actual program that asked me to type 'end of file' when I finished entering the student homework grades... sorry if I was being a bit thorough, but I prefer to make sure someone can follow the instructions a program gives on-screen rather than rely on their knowledge of computer languages little quirks :)

EDIT: in real world I'd expect a prog like that to end-up in the hands of a teacher, and even if their teaching c++, I'd not expect them to know keyb shortcuts or esc sequences (seeing how incompetent they seem to be) ;)

Yeah I know what you mean, I only did it because the book said to. I always prefer to do a more obvious way to terminate a program :)
Was This Post Helpful? 0
  • +
  • -

#12 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: EOF and cin

Posted 19 January 2011 - 03:47 PM

I think you're being a bit pedantic there I_cant_C. Most programmers know that ^D (on linux) ^Z (windows) is end of file, or they'll know the one for their system.

Typing in "end of file" was just foolish.

This post has been edited by simeesta: 19 January 2011 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#13 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 03:59 PM

I know this, but I am intrigued how ^Z can mean EOF - yet, when you actually type 'end of file' it can have a whole different meaning/ operation... I'm curious is all, I like the quirky things in life, and thus like to understand them - if I'm a fool for seeking understanding then I'll just continue being a fool if you don't mind. :)
Was This Post Helpful? 0
  • +
  • -

#14 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: EOF and cin

Posted 19 January 2011 - 04:11 PM

This may be useful to you. Also check out this.

I said you were foolish because I thought you knew the end of file was ^Z, and typing "end of file" was pedantic.

This post has been edited by simeesta: 19 January 2011 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#15 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: EOF and cin

Posted 19 January 2011 - 04:15 PM

I know I'm pedantic, and I can take things literally - have done all my life.

It's how at the age of around 5 (many moons ago) my mum had said: "put a clean pair of underwear on each day!".

Then a few days later came bath day - she asked: "why the heck have you got 5 pairs of underpants on!"... actually happened *hangs head in shame*

EDIT: after scanning those links, and trying "end of transmission" instead, still yielded the same results... curious...

This post has been edited by I_cant_C: 19 January 2011 - 04:22 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2