D
D
David Nigmatullin2019-08-23 22:53:48
Command line
David Nigmatullin, 2019-08-23 22:53:48

How to adequately delay console in C++ Sublime Text?

I decided to try something simple instead of all these cumbersome IDEs. I decided to attach MinGW to Sublime Text so that I could compile and run .cpp directly from it. Everything works, but there is such an obvious problem: after the program ends, the console immediately closes, and does not wait for Enter, well, this is understandable, it should be, and it is even treated using system("pause"). Noo, this is very unpleasant, as a perfectionist I just can't do it. Everything works like this:
1) You install Sublime Text
2) You install MinGW
3) You need to go to Control Panel->System and Security->System->Advanced system settings->Environment Variables
4) In the "system variables" section, find the variable " path".
5) Add to it, separated by a semicolon (everywhere without spaces), the path where the MinGW compiler is located, usually it is in the root of the C drive in the MinGW-> bin folder. Or in Windows 10 (maybe not only), you can simply add it to the list.
After that we need to add Build System (In sublimeText - Tools->Build system -> and select). There is a ready-made system there, but I found how to open the console, and so that everything is done there:
1) You need to create a file
"c:\Users\%UserName%\AppData\Roaming\Sublime Text 3\Packages\User\%name Build System%.sublime-build (I have MinGW.sublime-build)"
2) Open this file with notepad and paste it there

{
   "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
   "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
   "working_dir": "${file_path}",
   "selector": "source.c, source.c++",
   "shell": true,
   "encoding": "1251",
   "variants":
     [
       {
         "name": "Run",
         "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "${file_base_name}"],
         "shell": true
       }
     ]
}

3) Save and select your Build-System with your name (I have MinGW)
After doing all this, you will find that everything works. But here the console closes, yes. And actually the question is this: Is it possible to somehow modify this Build-System file so that the console does not close, but waits for Enter? As far as I understand, this is Json, and I’m not really booming in it yet, just yesterday I did all this. If anything, here is the instruction by which I did all this:
www.cyberforum.ru/blogs/390663/blog1982.html

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dalp, 2020-11-28
@Dalp

As I understand it, you have nothing against the "pause" command, but simply don't want to write system("pause") in every .cpp file. In this case, you can line:

"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "${file_base_name}"],

replaced by:
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/K", "${file_path}/${file_base_name} && pause 'TTL'>Nul && exit"],

Here, after compilation, instead of running the code directly, cmd is first launched, after which the compiled .exe file is passed into it along with the pause command and then exit (to exit cmd).
'TTL'>Nul removes the standard text output by the pause command (To continue, press any key . . .), just otherwise this text "sticks" to the output of the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question