7

How can I use console input in SublimeText 2.0.1? I'v chosen "Tools -> Build System -> C++", and add hello.cpp file to the project:

#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;
}

Build successful, but when I run ("Tools->Run"), the line "std::cin >> a >> b;" is passed and I can't enter the values. In terminal with g++ it run well. OS: Ubuntu 12.04

Ilya Yalchyk
  • 73
  • 1
  • 5
  • Possible duplicate of [Sublime text 3 - compile program and run in terminal](https://stackoverflow.com/questions/21196077/sublime-text-3-compile-program-and-run-in-terminal) – jdhao Jun 29 '17 at 06:11

2 Answers2

2

I don't think stdin is supported in Sublime Text, however, you can create a file stdin.input and use it under the editor:

#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;
}
perreal
  • 94,503
  • 21
  • 155
  • 181
1

The only error I see is that your missing int c; And if that doesn't work maybe try return 0; instead of return 1;

  • You are right. But my problem with editor "Sublime Text". I'v just copied that's code inaccurately. I will correct my ask. (I mean that my code is compiling with g++ from terminal well) – Ilya Yalchyk Jul 22 '12 at 11:12