DEV Community

5hfT
5hfT

Posted on • Edited on

Live Input Output (Competitive Programming) setup in VS CODE for C/C++ (WINDOWS)

How to install and setup vs code for competitive programming with c/c++ for Windows Users

install vs code

  • Download vs code : Link
  • Install in your pc

Setup environment for c/c++

  • install mingw : link

  • In you pc search for system environment

search system enbironment

  • go to Environment variables

Environment variables

  • Go to system varibale and click on path

system varibale

  • click edit

  • select new

  • paste the link where you mingw bin folder . for me it is C:\MinGW\bin

    • find it there
  • select ok ok for everything

Install git

  • download link Link

Then

  • Open Vs code

  • Open your coding folder file->open folder

  • press ctrl+~ and the terminal will be opened at the bottom

terminal

  • if in the terminal u find that its cmd or powershell then click on cmd/powershell and select 'set default sell' and select git

    • Now click on the delete icon and press ctrl+~ and its saying your shell is bash!
  • if still bash no showing then close vs code and open vs code again

Run a programme for testing :

  • create a file hello.cpp
  • write code to print hello
  • in terminal use the command

    g++ hello.cpp -o test && ./test
    
  • If you find hello then congrats

  • if saying g++ not found then try to close vs code and then oepn vs code again.

Setup Vs Code

  • Clik on terminal (At the top)
    Top bar

  • select configure task

  • select create task.json from template

  • select other and new task.json file will be created

  • now copy and paste the my task(windows).json {link is here} file into your task.json file

  • done

More things to do :
Note : You just need to do these things only for the first time of your workspace!

  • Split screen in 3 sections Split Screen
    • split the main section in right (right mouse click on the .cpp file and u will get the split options)
    • then split the right into down (same as above)
  • create a file named input.txt
  • create a file named output.txt
  • open input.txt into the upper right section
  • open output.txt into the lower right section

This will be the final look of your vs code

Final Look

Now create a folder and create your file .cpp inside the folder and give inputs in input.txt and press Ctrl+Shift+b and your output will be in the output.txt file

Note : Don't keep the cpp file in the base folder. I mean you have to create one or more folders in the base folder (Base folder means where you opened your vs code or you can say vs code wrokspace folder).and the input and output file should be in the base folder.

Folders should be like this or similer :

VS CODE Folders

Not This

Press Ctrl+Shift+b and your output will be in the output.txt file

Happy coding :3

Top comments (44)

Collapse
 
viissgg profile image
Vijay

Hey. Thanks for this nice guidelines. I'm getting below error. Can you please help ?

> Executing task:  cp d:\extra\cpp\hello.cpp D:\extra\cpp && g++ hello.cpp -o test.out && ./test.out < input.txt > output.txt && rm *out && rm hello.cpp <

cp: cannot stat 'd:extracpphello.cpp': No such file or directory
The terminal process "C:\Program Files\Git\bin\bash.exe '-c', ' cp d:\extra\cpp\hello.cpp D:\extra\cpp && g++ hello.cpp -o test.out && ./test.out < input.txt > output.txt && rm *out && rm hello.cpp'" failed to launch (exit code: 1).

Terminal will be reused by tasks, press any key to close it.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mh_shifat profile image
5hfT

Recently i have added some changes for windows hope this will help. Please check this out

github.com/jspw/VS-Code-Config/iss...

Collapse
 
viissgg profile image
Vijay

Hmm. I moved the cpp file in there own folder as mentioned in the link. But still no luck :(

Thread Thread
 
mh_shifat profile image
5hfT

give me a ss of your base folder

Thread Thread
 
viissgg profile image
Vijay
Thread Thread
 
mh_shifat profile image
5hfT • Edited

did you change the task.json file try this as i have said that i make some edits.

Thread Thread
 
viissgg profile image
Vijay

I can see your last commit is 4 days ago. I'm using the recent one. Still not working. Do I need to use the older one ?

Thread Thread
 
mh_shifat profile image
5hfT

Note : Make sure your mouse cursor is clicked or focused on the cpp file editor while you are running your code or pressing control+shift+b

Thread Thread
 
mh_shifat profile image
5hfT

The error you are facing must be solved using the updated task.json file for windows.

Thread Thread
 
viissgg profile image
Vijay

I'm focusing the file while running it. Also I'm using the latest file.

dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
 
viissgg profile image
Vijay

With closer look on the error. I feel like git shell in not converting file path properly.

${file} -> d:\extra\cpp\cod\hello.cpp (I guess this one not converting well. Notice the directory name case)
${workspaceFolder} -> D:\extra\cpp (Correct one)

Thread Thread
 
viissgg profile image
Vijay • Edited

I figured it out. Problem was same. Git shell was not recognizing the path. The path parameter should be in quotes. Below is the fix.

In args, make these changes.

"${file}" -> "'${file}'"
"${workspaceFolder}" -> "'${workspaceFolder}'"
"${fileBasename'" -> "'${fileBasename}'",

Enter fullscreen mode Exit fullscreen mode
"args": [
        "cp",
        "'${file}'",
        "'${workspaceFolder}'",
        "&&",
        "g++",
        "'${fileBasename}'",
        "-o",
        "test.out",
        "&&",
        "./test.out",
        "<",
        "input.txt",
        ">",
        "output.txt",
        "&&",
        "rm",
        "*out",
        "&&",
        "rm",
        "'${fileBasename}'"
      ],
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
mh_shifat profile image
5hfT

thats great if you have figured it out but i have tested my previous task.json file and it is working fine with other machines!
I think windows have some issues with file management system though its all working file in Linux.

Thread Thread
 
akib35 profile image
akib35 • Edited

I had same problems too. After adding extra ( ' ' ) made it successful suggested by kikit.
Thanks.

Thread Thread
 
mh_shifat profile image
5hfT

Most welcome.

Thread Thread
 
mh_shifat profile image
5hfT

I will check it and will update the repo

Collapse
 
mahdihasan profile image
Mahdi Hasan • Edited

this modification in task.json is working with git bash

"args": [
          "cp",
          "'${file}'",
          "'${workspaceFolder}\\${fileBasename}'",
          "&&",
          "fileName=\"${fileBasename}\"",
          "&&",
          "objectFile=\"${fileName%.*}.out\"",          
          "&&",
          "g++",
          "'${fileBasename}'",
          "-o",
          "${objectFile}",
          "&&",
          "./${objectFile}",
          "<",
          "input.txt",
          ">",
          "output.txt",
          "&&",
          "rm",
          "${objectFile}",
          "&&",
          "rm",
          "${fileBasename}",
        ]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
soham04 profile image
soham04

Your .json file link is not working, I got my work stoped in between due to that !!!

Collapse
 
mh_shifat profile image
5hfT

Did you use this one ?

Collapse
 
soham04 profile image
soham04

these tasks.json will be only valid for this folder and its subfolder only ?

Thread Thread
 
mh_shifat profile image
5hfT

not like that. Let me see you this

for different workspaces you have to make the tasks.json file once.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
mh_shifat profile image
5hfT

What do u mean setup vim??

Collapse
 
erenyaeger profile image
Eren Yaeger

Image description

Image description

This is what I'm getting...

Collapse
 
mh_shifat profile image
5hfT

what is your default shell in this vscode space ?

Collapse
 
virajasmane profile image
Virajas Mane

It is showing hello.cpp : No such file or directory
and also fatal error no input files
can you please help??

Collapse
 
mh_shifat profile image
5hfT

did you create input.txt in the same directory as the hello.cpp??

Collapse
 
soham04 profile image
soham04

Will is save out code in github repo also ?

Collapse
 
amrita_kadam_fd9ffd175696 profile image
Amrita Kadam

Yes thank you so much I just learned that I did not create a folder in the base folder and then the file.cpp.
This worked.

Collapse
 
erenyaeger profile image
Eren Yaeger

Please someone help me out as this is not working in my vscode

Collapse
 
saksham08jain profile image
saksham08jain

Image description
I am getting this error,any help is greatly appreciated

Collapse
 
evils_paradise profile image
Rooghz
[
        "cp",
        "\"${file}\"",
        "${workspaceFolder}\\jspwTest.cpp",
        "&&",
        "g++",
        "${workspaceFolder}\\jspwTest.cpp",
        "-o",
        "${workspaceFolder}\\jspwTest",
        "&&",
        "${workspaceFolder}\\jspwTest",
        "<",
        "input.txt",
        ">",
        "output.txt",
        "&&",
        "rm",
        "${workspaceFolder}\\jspwTest.exe",
        "&&",
        "rm",
        "${workspaceFolder}\\jspwTest.cpp"
      ]
Enter fullscreen mode Exit fullscreen mode

Try this it will work..You just have to mention where this 'jspwTest.exe/cpp' file will be present.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.