2012-01-27 49 views
0
#include "ellison.h" 

int main(int argc, char *argv[]) 
{ 
    if (argc > 1) 
    { 
     int errorOutput = Execute(argc, argv); 
     switch (errorOutput) 
     { 
      case 0: 
       return EXIT_SUCCESS; 
       break; 
      default: 
       cout << "An error occured: " << ParseError(errorOutput); 
       return ERROR; 
       break; 
     } 
    } 

    cout << "+---------------+ \n"; 
    cout << "| ellison 0.1.1 | \n"; 
    cout << "+---------------+ \n\n"; 

    int errorOutput = 0; 
    string input; 

    while (true) 
    { 
     cout << ">"; 
     input = ""; 
     cin >> input; 

     if (input == "quit") 
     { 
      if (errorOutput != 0) 
       return ERROR; 
      else 
       return EXIT_SUCCESS; 
     } 

     errorOutput = Execute(input); 
     switch (errorOutput) 
     { 
      case 0: 
       break; 
      default: 
       cout << "An error occured: " << ParseError(errorOutput); 
       break; 
     } 
    } 
} 

此代碼編譯並運行正常。奇怪的是,如果我用一個或多個空格鍵入一長串字母,我有兩個大於號的符號,而不是一個。我是否有某種錯誤? 我會補充說這不適用於短輸入字符串,並且這是用Visual-C++ 2012編譯的在命令行應用程序中輸入有問題

回答

0

std :: cin只讀直到第一個空格。然後,它不刷新讀取緩衝區。 因此,由於您的「while(true)」,當它到達第二個std :: cin時,它將讀取輸入的第二部分,而不用等待新的輸入。 您應該試試std :: getline(stream & readingBuffer,std :: string & destination)。它會讀取整條生產線,像這樣的:

getline(cin, input); 

只是不,除非你使用cin.ignore(1)使用CIN後使用cin和函數getline在同一時間,因爲CIN留下「/ N」在流中。如果您在同一個流中使用getline,它將停止在'/ n'處,而不是讀取您想要讀取的內容。

+0

謝謝。那是我需要的。 – Andonuts 2012-01-27 06:01:13

相關問題