2015-10-15 80 views
1
//Program to terminate the input by ctrl-c  
#include<iostream> 
#include<vector> 
#include<string> 

using namespace std; 

int main() 
{ 
    vector<string> vec; 
    string word; 

    while (cin >> word) 
     vec.push_back(word); 

    cout << endl << endl; 
    auto beg = vec.begin(); 
    while (beg != vec.end()) 
    { 
     //to print the vector 
     cout << *beg << endl; 
     beg++; 
    } 
} 

当减少印刷机控制-c终止的输入流...当我使用Ctrl-C终止输入,输出在Visual Studio 2013

ankur 
anshu 
singh 
ankit 
ashuto 
ashu 

我得到。 ...

ankur 
anshu 
si^CPress any key to continue . . . 
+0

只是为了好奇,你还期望发生什么? – user463035818

+1

Ctrl + C终止程序,而不是输入。改为使用Ctrl + Z。 –

回答

4

在终端(aka命令行)中,Ctrl+C立即杀死程序。它不会等待任何事情或任何人。因此,你的程序在执行过程中会停下来,所以它所做的和不做的事情就是彻头彻尾的。

在某些情况下,您可能确实需要Ctrl+D(Linux/Unix)或Ctrl+Z(Windows),它表示“文档结束”。这可能会被解释为“输入结束”,而不会中断程序。但是,您的结果可能有所不同。

+1

unix中的Ctrl + D是Windows中的Ctrl + Z –

+0

啊,谢谢。我补充说。 – CodeMouse92

+0

感谢您的回答 – Ankur