How to terminate a program with Ctrl-D?

后端 未结 5 1267
太阳男子
太阳男子 2021-01-20 01:06

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 se

5条回答
  •  别那么骄傲
    2021-01-20 01:42

    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++.

    Is this homework, by the way? If so, please be sure to tag it as such.

提交回复
热议问题