如何使用 Ctrl-D 终止程序?

2023-11-23

I am trying to write a simple program that simulates a calculator. I would like the program to exit or turn-off when the Ctrl+D keystroke is made. I searched through stackoverflow and saw other examples of Ctrl+C or Ctrl+A but the examples are in java and C.

for C:

(scanf("%lf", &var);

for java, a SIGINT is raised when Ctrl+Z is pressed.

signal(SIGINT,leave);  
    for(;;) getchar();

I am wondering what can I do for Ctrl+D in C++...

感谢大家!


Ctrl+D will cause the stdin file descriptor to return end-of-file. Any input-reading function will reflect this, and you can then exit the program when you reach end-of-file. By the way, the C example should work verbatim in C++, though it may not be the most idiomatic C++.

顺便问一下,这是作业吗?如果是这样,请务必对其进行标记。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Ctrl-D 终止程序? 的相关文章

随机推荐