c++从控制台循环读入字符串,如何终止循环并正常执行后面的程序?
大家讲道理
大家讲道理 2017-04-17 13:45:13
0
2
614
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    //file();
    vector<string> coll;
    copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(coll));
    sort(coll.begin(), coll.end());
    unique_copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));

    return 0;
}

如果从文件读入,可以读入字符串并终止程序,并且正常执行后面的程序,就是可以打印出读入的字符串。但是如果是从控制台读入,就无法终止了,不知道这个时候该怎可办才能够让程序停止读入并正常执行下去。如果直接ctrl+c,就是把整个程序都直接终止了。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
Ty80

ctrl z under Windows, ctrl d under *nix can send EOF to the program

迷茫

You can set a condition to exit the loop. For example, if you read the string quit, you will exit the loop and continue execution. Librazy's answer is the correct answer.

If you need to implement Ctrl+C to terminate the loop and continue the program outside the loop, you can use signal. This is a set of mechanisms provided by the operating system. When you press Ctrl+C, a signal is actually sent to the process.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template