Sublime Text,带有c ++程序的控制台输入

时间:2012-07-22 09:20:00

标签: c++ gcc sublimetext2

如何在SublimeText 2.0.1中使用控制台输入? 我选择了“工具 - >构建系统 - > C ++”,并将hello.cpp文件添加到项目中:

#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&gt;&gt; a&gt;&gt; b;”传递,我不能输入值。 在带有g ++的终端中运行良好。 操作系统:Ubuntu 12.04

2 个答案:

答案 0 :(得分:2)

我认为Sublime Text不支持stdin,但是,你可以创建一个文件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;
}

答案 1 :(得分:1)

我看到的唯一错误就是你缺少的int c; 如果这不起作用,可以尝试返回0;而不是返回1;