CSCI 240
Using the OnlineGDB 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. The new recommendation for CSCI 240 students is the compiler available at onlinegdb.com.

https://www.onlinegdb.com/

Running a Program

  1. Set the programming language to C++14 in the dropdown menu that is located above the upper right corner of the editor screen.

  2. Type in the following C++ source code/program in the editor window, making sure to fill in the Programmer, Section, and Due Date. You can also modify the code that is given when the onlinegdb.com page loads to match what is below:

  3. /***************************************************************
    CSCI 240         Program 0     Spring 2023
    
    Programmer:
    
    Section:
    
    Date Due:
    
    Purpose: Compiler training
    ***************************************************************/
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    cout << "hello, world";
    
    return 0;
    }
  4. To save a copy of your source code, click the button with the downward pointing arrow that is located above the editor window. This will create a file named main.cpp in the default location for your browser downloads. An alternative is to click the Save button, but this requires a Google+, Facebook, or Github account to save the program.

  5. Make sure the radio button with Interactive Console is selected. This is located below the editor window with the source code. This selection will allow the program to be run with interactive user input (if that is something that the program uses).

  6. Now compile and run the program. Click the Run button that is above the editor window with the source code. After a few seconds, you should see some results. A box with the output that is produced by the source code should appear below the editor window. If it does not, you have a compile error on the line that is specified in the error message. You must find and fix all such errors, and re-run until you get the program output.

  7. The output that is produced by the program listed above is "hello, world". When you're done viewing the output, you can hit Enter.

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

  9. 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.

  10. 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 the Compilation failed due to the following error(s) message displays and there are several errors listed. 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 20 if you typed in the program as shown) which will usually indicate the location of the error.

  11. 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'".

  12. 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.)

  13. 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.

  14. Fix the program, run it again.

  15. 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 Upload File button (it's the one with the cloud and arrow that point upward). Locate the file that was downloaded (probably named main.cpp).

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

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

    Run, check it, save, and quit.

    That's enough for one day.

  18. 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 onlineGDB's compiler, simply type the information in the output window when a cursor appears.