我不太明白如何在C++程序中使用’EOF’来结束。

huangapple go评论62阅读模式
英文:

I don't quite understand how to end C++ program with 'EOF'

问题

作为一名C++初学者,我尝试解决一些初学者问题,比如'Hashmat the Brave Warrior' UVA问题。这个问题要求我输出输入中2个数字的差异,输入将以"End of File"终止。

示例输入:

10 12
10 14
100 200

输出:

2
4
100

这是我的代码:

#include <iostream>
using namespace std;

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

即使我没有输入任何内容,程序仍在运行,因此这个解决方案对该问题是错误的。所以当我上传我的解决方案到virtualjudge账户时,他们将其解释为错误的解决方案。我如何使程序在"End of File"时结束?

英文:

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 &lt;iostream&gt;
using namespace std;

int main(){
    int a, b;
    while (cin &gt;&gt; a &gt;&gt; b){
        cout &lt;&lt; max(a,b) - min(a,b) &lt;&lt; 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'?

答案1

得分: 1

操作系统不会仅因为您没有输入任何内容而生成EOF;它会等待您输入内容所需的时间。您的程序将继续运行,等待输入。

从交互式会话中指示EOF的方式将取决于操作系统。如在Windows的注释中所提到,您需要输入Ctrl-Z,而在Linux或Mac上,您需要输入Ctrl-D。之后您可能还需要按Enter键。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月7日 12:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定