2016-01-08 277 views
0

所以我只是做一個程序,接受輸入並遇到錯誤;我嘗試過調試,但是當我使用std :: cout時,沒有消息讓我調試它。控制檯沒有響應?

int main() 
{ 
    try { 
     std::ifstream text_file; 
     ics::safe_open(text_file,"Enter file name to analyze","C:\\Users\\Xari\\Downloads\\program1\\graph1.txt"); 
     Graph graph = read_graph(text_file); 

     print_graph(graph); 

     while (true) { 
      std::string input; 
      std::cout << "Enter the name of a starting node (enter quit to quit) D: "; 
      std::cin >> input; 
      std::cout << "INVISIBLE DEBUGGER MESSAGE" << std::endl; 
      if (input != "quit") 
       std::cout << "Reachable from node name " << input << " = " << reachable(graph, input) << std::endl; 
      else 
       break; 
     } 

    } catch (ics::IcsError& e) { 
     std::cout << e.what() << std::endl; 
    } 

    return 0; 
} 

問題是如果我輸入「a」,「b」輸出很好。如果我輸入c或更高,我不會收到消息回覆。即使是更奇怪的是,如果我在我的意見之後說得對,我仍然會得到同樣的問題,這是沒有意義的。

+0

錯誤最有可能在可達函數中。如果你使用調試器,你可以看到。 – Joni

+0

您的'Graph'可能已損壞,其遍歷功能有缺陷,或者'reachable'算法有缺陷。我們無法從您發佈的代碼中知道。你應該發佈一個最小但完整的,可編譯的程序來重現這個問題:在這種情況下,如果在print_graph調用之後簡單地調用std :: cout << reachable(graph,「c」),你可以完全拋棄輸入的東西。 ;'掛了。 –

+0

也許嘗試使用'std :: getline(std :: cin,input);'而不是'std :: cin >> input;''getline()'會等到用戶輸入'ENTER',然後返回整個行輸入,而當用戶鍵入一個空白字符時,'std :: cin >>'將停止閱讀。 –

回答

1

檢查是否可達(圖形,輸入)不會遇到無限循環

+0

哦所以std :: cout不會列出,即使它在可達之前被調用? –

+0

@ Xari:你說你沒看到'std :: cout <<「INVISIBLE DEBUGGER MESSAGE」<< std :: endl;'output?但是你看到'std :: cout <<「輸入一個起始節點的名字(輸入quit退出)D:」;'提示符並輸入一些內容,然後按ENTER鍵? –

+0

是的。我看到「輸入名稱....」。當我進入它只是移動到一個新的行,並沒有做任何事情。只有當我輸入「a」或「b」時,它纔會通過可達性。否則,前者發生。 –