7

如何在 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 >> a >> b;”行 已通过,我无法输入值。在带有 g++ 的终端中,它运行良好。操作系统:Ubuntu 12.04

4

2 回答 2

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;
}
于 2012-07-22T11:12:24.737 回答
1

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

于 2012-07-22T10:31:39.820 回答