2015-10-15 79 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