Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Configuring Replit

Configuring Replit

Replit is a browser based editor that allows you to write and compile programs with cloud storage without any downloads. It is the standard compiler that many of our c++ based labs use.

To start with, Replit has a navigation menu on the far left. There are a few useful buttons here:
A screenshot of the navigation menu on the left side of the Replit website.  It has buttons labelled create, upgrade, home, template, my repls, community, learn, teams, and curriculum.  At the bottom is a sun icon that changes colour theme.

  1. Create allows you to save a new public repl on your account in the selected language
  2. My Repls allows you to view any public repls you have previously saved
  3. Teams is where the lab repls are stored. Some labs will require you to perform and submit your repls in this tab
  4. The sun/moon in the bottom right corner allows you to toggle between light and dark theme

Inside a Replit, by default you will have your files on the left, your editor in the center, and the console, which displays the output of your program, on the right. You can resize the divisions between these sections by clicking and dragging on the border between them.

On the left of the files section, there is a settings menu. From here, you can customize more features of your repl, such as indent size, line wrapping, and code intelligence, which is the autocomplete Replit uses.

Running programs in Replit

There are three ways you can run a program in Replit

Executing main.cpp with the run button

By default, replit will run whatever file has "int main()" defined in it. This is fine if this is all you want, but often you will have different files with main functions. If you try to press "run" while you have multiple files with main, you will get an error and it won't work. While you can temporarily rename the main function in files you aren't using, you would likely lose marks if you forgot to change them back later

Running programs from command line

You can determine exactly which files you want to run by using the gnu debugger in command line with the following format:

g++ file_to_run.cpp -o name_of_output_file
./name_of_output_file

Modifying the .replit hidden file

Replit has a hidden file named .replit that determines the behaviour of the run button. If you want to determine which file you want to run without having to write it in the console each time, you could change the .replit file to execute the g++ command instead.

  1. First, you need to unhide the file. In the top right of the files section, click the dots and click "show hidden files"
  2. Without changing other files, open the .replit file and delete everything on it. Replace it with the following format:
    language = "c++"
    run = "g++ file_to_run.cpp -o name_of_output_file; ./name_of_output_file"

Notice that the format for this is the same as the one used in the console, but it combines the two steps into one.

Knowing how to configure your .replit file is important for using the debugger, which is covered next week.

Exercise

The goal of this exercise is to make sure you understand each of the compilation methods

  1. Create a new repl for c++. Change "hello world" to say "file one" and run it. The console should read "file one" after the program has executed
  2. Create a second file named test.cpp. Copy everything from main.cpp into it, then change the output to "file two" - if you were to click the run button now, it would generate an error because you have two main functions
  3. You can clear the console by writing "clear" into it. Then enter the following:
    g++ main.cpp -o main;
    ./main
    Which should produce the output "file one", and if you replace main.cpp with test.cpp, it would produce "file two"
  4. Show hidden files by clicking the dots on the top right of the files menu and clicking "show hidden files"
  5. Open the .replit file, remove everything, and change it to
    language = "c++"
    run = "g++ main.cpp -o main; ./main"

    Click the run button, and the output should say "file one"
  6. Change the .replit file to
    language = "c++"
    run = "g++ test.cpp -o test; ./test"
    Which will produce the output "file two"