Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text with console input for c++ programs

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

like image 592
Ilya Yalchyk Avatar asked Jul 22 '12 09:07

Ilya Yalchyk


1 Answers

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;
}
like image 103
perreal Avatar answered Oct 05 '22 21:10

perreal