Exercise 1: Roar

This assignment is about installing CLion and the associated C and C++ compilers. Do this before classtime to avoid flooding the local network. These directions assume you are using a Windows box such as your school computer; Mac and Linux users can ask your instructor for help if they need it, but CLion is pretty easy to install on those systems as well.

  1. If you have not done so yet, create a (free) student account with JetBrains by visiting the student account page and clicking on the Apply now button. The simplest is to use your school email address. Let your instructor know if you have problems with this step.
  2. Visit https://www.jetbrains.com/clion/. If necessary, log in to your account. Make sure the right operating system is selected (Windows/macOS/Linux) and click on the Download button.
  3. Install the downloaded executable. If necessary, give it permission to change your system. When you get to Installation options, you can accept the defaults but consider the following:
    • You may want to enable Add "Open Folder as Project" to the menus, but you can also open folders from within CLion if you prefer to not have the extra options in your context menus.
    • Many users do not allow it to create associations with C++ or C source files. It is much faster to use a tool like NotePad++ to view code. But enabling these will not break anything.
  4. Complete the installation. This includes rebooting your laptop.
  5. In Windows, the default is to hide the extensions on file names. This is not useful for C++ programmers (or really any student in the engineering fields) because you will encounter lots of unregistered file types. To fix this:
    1. Hold down the Windows key and press 'e'. This opens a File Explorer window.
    2. Click on the View menu.
    3. In the Show/hide group (far right), put a check mark in File name extensions.
    4. Less critical, but you might also put a check mark in Hidden items.
  6. Open CLion. CLion includes a module that might provide good help for professionals but is misleading to students in this class. Visit Settings..., Editor, Inspections, C/C++, Static Analysis Tools, and disable Clang-Tidy.
  7. In CLion, create a new project:
    1. From the File menu, select New and then Project....
    2. Set the Location to a reasonable location. The default is CLionProjects\untitled, but you will want to change that. We highly recommend creating a Git repository for the class and putting all of your assignments in that repository. Ask for help creating the repository if you need it. You would then put the code for this assignment under a ex1 folder in this repository.
    3. You will likely get a popup in the lower right corner of CLion talking about your Microsoft Defender configuration. Click on Automatically and approve allowing elevator.exe to make changes to your computer. If you miss the box, you can find it in the notifications section of CLion by clicking on the bell in the upper right corner.
    4. Open main.cpp and click on the green run arrow. You may get a prompt about using the built-in compiler, MingW. Accept the defaults. When your program runs, you will see
              Hello, World!
      
      in the Run window.
  8. Modify your program so it reads a number from the user and prints its cube. A sample run will be the following:
            Enter a number: 5
            The cube of 5 is 125
    
    (where the user entered the 5). In particular, add a using namespace std; below the #include, change the output line to print "Enter a number: " (don't forget the space after the colon and to delete the endl at the end of the line), add a declaration for an integer variable, read that variable, then print the last line. Absolutely ask for help if those directions are clear to you!! Your neighbor is a fine source for this - there is no concern about plagiarism with exercises like this. Test that your code works on other inputs like 0 and -3.
    You may notice that it seems there's no space after the colon when you run the program; this seems to be a feature of how CLion runs code; the space will be there in other contexts.
  9. So you know how, set a break point at the cin line in your code by clicking on the line number. Click on the bug icon at the top of the window and notce that it shows the value of your variable before the cin statement. The value may be 0, but it might also be some random value. Step one statement and go to the Console window to enter a value. You might even try entering something like "6x3" (that is, a number with an illegal character in it). If you do, notice the program runs without errors. C++ is like most languages in not automatically checking inputs for validity.
  10. For full credit for this assignment, demonstrate your solution to your instructor. You can go on with the next step while waiting for your instructor to check you off.
  11. It is often very convenient to be able to build and run programs outside of CLion. It's not required, but it can be helpful to install a standalone C++ compiler. A simple way to do this is to install Chocolatey from https://chocolatey.org/ (follow the directions to use an administrative shell) and then type
            choco install mingw
    
    I also choco install gow to get the "Gnu On Windows" library that provides Windows versions of many Unix commands. You can then open a command prompt, cd to the appropriate folder, and type
            g++ main.cpp
            a.exe
    
    to run your program. We will discuss this in more detail later in the term.
  12. The default formatting is fine for this course, but following the Google standard I set the default line indentation to 2. Click on the "hamburger" menu (three horizontal lines in the upper corner), select File | Settings..., open the Editor and Code Style items, click on C/C++, and change Tab Size, Indent, and other items appropriately. Do not enable "Use tab character" - putting tab characters in source files is very poor practice.