0

I wrote c++ code in the codeblocks IDE. I use EOF but don't no which keyword i will use to exit form my code. Here is my code:

int main() {
    int a, b;
    while ( scanf("%d%d",&a,&b)!=EOF) {
        printf ( "%d\n", a^b);
    } 
}
James Z
  • 12,209
  • 10
  • 24
  • 44
mdshohed
  • 7
  • 5
  • It depends on your system. I believe for windows it is Ctrl + Z – smac89 Mar 04 '21 at 18:20
  • To close the stdin pipe in Linux, you need to Ctrl+D. – Rohan Bari Mar 04 '21 at 18:23
  • Does this answer your question? [How to enter the value of EOF in the terminal](https://stackoverflow.com/questions/11968558/how-to-enter-the-value-of-eof-in-the-terminal), it's C duplicate but it works the same way. – anastaciu Mar 04 '21 at 18:28
  • The program is incorrect, it should check `== 2` rather than `!= EOF` – M.M Mar 04 '21 at 21:30

2 Answers2

2

The "scanf" return value is EOF for an error, or if the end-of-file character or the end-of-string character is found in the first attempt to read a character.

You can use "Ctrl + Z" in windows or "ctrl +D" for linux.

0

If you are using windows CTRL + C is the code for EOF() as Enter/Return key would add a newline only

iceweasel
  • 756
  • 1
  • 6
  • 11
  • yeap brother. It's working for CTRL + C. Thanks a lot. – mdshohed Mar 04 '21 at 18:44
  • @mdshohed This isn't the right answer. Ctrl+C kills the whole program immediately, it doesn't make `scanf` return EOF. OP should be using Ctrl+Z, as the other answer suggests. – HolyBlackCat Mar 04 '21 at 19:56