2012-07-22 53 views
7

如何在SublimeText 2.0.1中使用控制檯輸入? 心中已經選擇 「工具 - >構建系統 - > C++」,並HELLO.CPP文件添加到項目:用C++程序控制臺輸入的崇高文本

#include <iostream> 
int main() 
{ 
    int a, b, c; 
    std::cout << "Enter: "; 
    std::cin >> a >> b; 
    c = a + b; 
    std::cout << a << '+' << b << '=' << c << std::endl; 
    return 0; 
} 

構建成功,但是當我運行( 「工具 - >運行」)時,行「std :: cin >> a >> b;」已通過,我無法輸入值。在終端上使用g ++它運行良好。 操作系統:Ubuntu的12.04

+0

的可能的複製[崇高的文本3 - 編譯程序並運行終端](https://stackoverflow.com/questions/21196077/sublime-text-3-compile-program-and-run-in-terminal) – jdhao 2017-06-29 06:11:41

回答

2

我不認爲標準輸入的文本崇高的支持,但是,你可以創建一個文件stdin.input和編輯器中使用它:

#include <iostream> 
#include <fstream> 

#define SUBLIME 

#if defined SUBLIME 
# define ISTREAM ifile 
#else 
# define ISTREAM std::cin 
#endif 

int main() 
{ 
    int a, b, c; 
    std::cout << "Enter: "; 
    #if defined (SUBLIME) 
     std::ifstream ifile("stdin.input"); 
    #endif 
    ISTREAM >> a >> b; 
    c = a + b; 
    std::cout << a << '+' << b << '=' << c << std::endl; 
    return 0; 
} 
+0

謝謝!有用。 – 2012-07-22 11:19:52

1

我看到的唯一錯誤是,你的缺失int c; 如果這不起作用,也許嘗試返回0;而不是返回1;

+0

你是對的。但我的編輯「崇高文本」的問題。我只是複製了不準確的代碼。我會糾正我的問題。 (我的意思是我的代碼是用g ++從終端編譯好的) – 2012-07-22 11:12:52

相關問題