0

As a C++ beginner, I tried solving some beginner problems such as the 'Hashmat the Brave Warrior' UVA problem. This problem requires me to output the difference between 2 numbers in an input, where the input will be terminated by "End of File".

Example input:

10 12
10 14
100 200

output:

2
4
100

This is my code:

#include <iostream>
using namespace std;

int main(){
    int a, b;
    while (cin >> a >> b){
        cout << max(a,b) - min(a,b) << endl;
    }
}

Even though I entered nothing, the program is still running, therefore this solution is wrong for the problem. So when I upload my solution the virtualjudge account, they interpret it as wrong solution. How do I make the program such that it ends with 'End of File'?

  • Are you sure this solution is wrong? Have you tried running it as `prog – pts Mar 07 '23 at 04:36
  • If you're still capable of typing input, then your program hasn't reached EOF. If you're on a terminal, you can do something like `echo 10 12 10 14 100 200 | ./your-program` – Stephen Newell Mar 07 '23 at 04:36
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 07 '23 at 04:37
  • 4
    on windows `ctrl-z` on linux or mac `ctrl-d` – pm100 Mar 07 '23 at 04:38
  • @pm100: On Windows, it actually is `ENTER` followed by `CTRL-Z` followed by `ENTER`. Pressing `CTRL-Z` and pressing `ENTER` in the middle of a line will not work. – Andreas Wenzel Mar 07 '23 at 04:48
  • @AndreasWenzel that has not been my experience on Windows, are you sure? – Mark Ransom Mar 07 '23 at 04:50
  • @MarkRansom: I just tested it again, to be sure. I tested the loop `while ( getchar() != EOF );` and this loop does not break when I type `CTRL-Z` followed by ENTER, except when I do this at the start of the line. I tested this with MS Visual Studio 2017 on Windows 7. It is possible that newer versions of Windows behave differently. – Andreas Wenzel Mar 07 '23 at 04:56
  • @AndreasWenzel I have Windows 7 as well, but unfortunately Microsoft has killed my version of Visual Studio. I may have to go back to an extremely old version to try it out. – Mark Ransom Mar 07 '23 at 05:03
  • @MarkRansom: [This answer to another question](https://stackoverflow.com/a/31261931/12149471) also states that it is necessary to press `ENTER` followed by `CTRL-Z` followed by `ENTER`. However, I am not sure if this only applies to MS Visual Studio or also to MinGW. – Andreas Wenzel Mar 07 '23 at 05:10
  • Does this answer your question? [How to read until EOF from cin in C++](https://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c) – Cristik Mar 07 '23 at 11:12
  • @AndreasWenzel I finally got a test program running and you're correct. I'd consider that a defect in the standard library because it's at odds with how `copy con temp.txt` works. I believe it does the correct thing if you're reading from a file instead of the console, that will be my next test. – Mark Ransom Mar 07 '23 at 13:47

1 Answers1

1

The operating system will not generate EOF just because you're not typing anything; it will wait as long as it needs for you to enter something. Your program will continue to run, waiting for some input.

The way you indicate the EOF from an interactive session will depend on the operating system. As mentioned in the comments for Windows you must type Ctrl-Z, and for Linux or Mac you must type Ctrl-D. You may also need to type Enter after that.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 4
    On Windows, it actually is `ENTER` followed by `CTRL-Z` followed by `ENTER`. Pressing `CTRL-Z` and pressing `ENTER` in the middle of a line will not work, it must be at the start of a new line. – Andreas Wenzel Mar 07 '23 at 04:49