CSCI 240
Using the Online CodeChef Development Environment


There are many online C++ compilers for those that can't install a compiler on the device that is being used for the course. Many students from past CSCI 240 courses have recommended the C++ compiler that is available at codechef.com.

https://www.codechef.com/ide

Make sure that the dropdown menu has C++14 (Gcc 6.3) selected.

Running a Program

  1. Type in the following C++ source code/program in the window, making sure to fill in the Programmer, Section, and Due Date:

  2. /***************************************************************
    CSCI 240         Program 0     Fall 2021
    
    Programmer:
    
    Section:
    
    Date Due:
    
    Purpose: Compiler training
    ***************************************************************/
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    cout << "hello, world";
    
    return 0;
    }
  3. To save a copy of your source code, click the button with the downward pointing arrow. This will create a file named Ide.cpp in the default location for your browser downloads.

  4. Now compile and run the program. Click the Run button that is below the window with the source code. After a few seconds, you should see some results. The Status should read "Successfully Executed" and a box with the output that is produced by the source code. If it does not, you have a compile error on the line that is highlighted. You must find and fix all such errors, and re-run until you get the "Successfully Executed" status.

  5. The output that is produced by the program listed above is "hello, world"

  6. Congratulations. You have created and run your first C++ program.

  7. Sit back and relax for 15 seconds... now let's go on.

    First a recommendation...It is probably a good idea to make it a habit of occasionally downloading your source code as you're developing your programs to ensure that you don't accidentally lose any of your work.

  8. Now, just to see what will happen, let's make a couple of mistakes on purpose. Use the delete key to erase the closing " (double quote character) after the word world in your program. Now try to compile and run the program. Notice that Status indicates Compilation Error and the Compile Info shows several errors. This is common - one mistake may cause several error messages. Your usual response should be to try to fix the first error in your program (which is usually indicated by the first or first few messages) and re-run. In this case, the first error mentions "missing terminating " character" and a line number (line 19 if you typed in the program as shown) which will usually indicate the location of the error.

  9. Now try deleting the semicolon at the end of the same line. Run. You see the same error messages even though we know there are now two errors. This often happens: one error "hides" another. Restore the " but leave the ; missing. Re-run. Now you see the message "error: expected ';' before 'return'".

  10. Restore the ; and change the character string to: "\nhello, world" That's a backslash before the n. Run the program. Notice there is now a blank line at the top of the output. The sequence "\n" makes the output go down one line. (Try it again using a forward slash to see what will happen.)

  11. Misspell the word main. Make it "maain". Run. A Linker error should show up in the Compile Info. Read it. This illustrates that some error messages are not entirely clear.

  12. Fix the program, run it again.

  13. Download a copy of the source code. Refresh/reload the webpage so that it returns the original default code from when you first loaded the page. To continue working on an existing program, click the Open File button below the source code window. Locate the file that was downloaded (probably named Ide.cpp).

  14. Once the file is opened, add another line after the first cout line but before the return line:

  15. cout << "\ngoodbye for now..";

    Run, check it, save, and quit.

    That's enough for one day.

  16. The .cpp file is the one that will be submitted for grading on Blackboard.

Running a Program with User Input

Most programs will use some kind of input that will be used in calculations or displayed. To add input to a program that is being run in codechef's compiler, click the Custom Input checkbox that is to the left of the Run button.

A window with the label Custom Input should open. Type (or cut and paste) the expected input into the window.

Run the program.

If your program includes instructions that read input, they will read the values from the Custom Input box in the order that they were entered.