**Discussion 1** 08/25, 08/27 [(back to course webpage)](./mcs360_fall2020.html) # Setting up your C++ Compiler Before we start writing code in C++, we need to install some stuff on our PC's. **I strongly recommend following the steps below to install a local g++ compiler, as opposed to using some online compilers as they are usually trash.** Among the few local C++ compilers available I prefer GNU C++, shortened as g++. Here are some instructions on how to install the compiler. ## Linux Users If you're a linux user, you most probably already have `g++` installed. To test, open your terminal with `Ctrl+Alt+T` and check if `g++` is installed by just typing ~~~~~~~~bash g++ ~~~~~~~~ Into the command line. If you get some output that says `fatal error: no file inputted`, then you're good. Otherwise, you will have to install g++ via your package manager. For most ubuntu-based systems, doing ~~~~~~~~bash sudo apt install g++ ~~~~~~~~ will suffice. Just provide your root password and storm through the installation, confirming with `y` at each dialog. ## MAC Users MAC already comes with g++. Open your terminal with launchpad or finder or whatever. Type in ~~~~~~~~bash g++ ~~~~~~~~ Into the terminal window. If you get something like `fatal error: no file inputted`, then you have g++ already installed. Otherwise, it will prompt you whether you want to install developer tools, if they are missing. Just say yes to the prompt, and it should install the tools required to run C++. ## Windows Users Windows users have to follow a couple of steps. Since g++ is not native in windows, there are ways around it such as using _Windows Subsystem for Linux_. Here we will do it the oldschool way since I'm not sure how to install g++ otherwise... Follow these steps: + Open [this webpage](https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/). This should download the file `mingw-get-setup.exe` to your PC. + Run the downloaded .exe, and select a convenient location to install the package (usually it's `C:\mingw`) but you can change it. Select the `g++` package from there and mark for installation, we don't need the other tools. + Click Apply. This should download and install g++ to your specified folder. + Open the folder where you installed mingw using file explorer, and open the `bin` folder. Right click on `bin`, and copy address as text. + Open the start menu and type "Edit environment variables". Change the environment variables to add the directory path to the "Path" variable. OK, now if you start powershell or command prompt and type `g++`, you will get the same output as the MAC and Linux guys. # Downloading an IDE/Editor Great, now you have g++ installed. Now you need to have some way of writing your code. ## Option 1: Notepad++ (windows, advanced) This is a good lightweight text editor that you can use to write C++ programs. This way is slightly advanced, so if you're feeling out of the loop, check Option 4. It's the easiest way out of this predicament. If you're up for challenges, let's install Notepad++ via downloading the **installer** [here](https://notepad-plus-plus.org/downloads/v7.8.9/). ## Option 2: Command line editors (linux/mac, advanced) Mmm to be frank, if you're in linux, try using `vim` to write code. It'll be a good experience, albeit pretty hard at first. `vi` also exists on MAC. To create a C++ file, create a new directory somewhere (name it `mcs-360` or something idk), and `cd` the terminal to this directory. Alternatively use the explorer and Shift+Right Click, followed by "Open terminal here". To write a C++ file, you gotta do `vim test.cpp` or `vi test.cpp`. Inside the CPP file, change to INSERT mode using `i`, and copy paste the code given in Section 3.3 using `Shift+Insert`. Press `Escape` and type `:wq` to save the file. `w` stands for "write", `q` stands for "quit". ## Option 3: XCode (mac) Just open XCode and copy paste the code below in Section 3.3, save it to a directory you won't get confused by (name it `mcs-360` or something), and save it as `test.cpp`. ## Option 4: Jetbrains CLion (safest alternative for win/mac/linux) If all this is foreign to you, you can do well with just installing an IDE. An IDE does all this hocus-pocus for you and provides you with an interactive platform to write code in. I prefer the Jetbrains CLion IDE since it's free for students! Head over to [this page](https://www.jetbrains.com/clion/download/) and download the necessary package for your OS. This installs a trial version, to get the full version you can register yourself as a student in the Sign-in icon right beside the cart icon on the page header, and get the student license for free. Then when you run the installer you will be prompted to log in, and this should complete the installation. If your compiler is set up properly, CLion will automatically detect where your `g++` is situated. # Compiling and Running a C++ Hello World Program Whew, that was hard. We now have a compiler set up, and an IDE or the command line tools set up. We have a folder that we named `mcs-360`. ## Command line guide Create a new text file named `test.cpp`. If you're on windows, please do enable file extensions via the File Explorer Settings, otherwise it'll be named as `test.cpp.txt`, and we all know how irritating it is. Inside the `.cpp` file, paste the code that follows... Now open powershell/cmd/terminal in the `mcs-360` directory where your `test.cpp` file is, and write the following commands: ~~~~~~~~bash g++ -o test test.cpp ./test ~~~~~~~~ If you're on windows replace the second line with ~~~~~~~~bash test.exe ~~~~~~~~ You should be able to see the desired output! :D ## CLion guide Create a new C++ project in the `mcs-360` directory you just made, and name it `test`, and copy paste the following code into `main.cpp` (select it from the sidebar in CLion). Run the project using the green "Run" icon on the top right or the bottom left corners. The output shows up in the bottom panel. ## 3.3. `test.cpp` code ~~~~~~~~cpp #include int main() { std::cout<<"Hello World!\n"; return 0; } ~~~~~~~~ # Exercise: Fahrenheit to Celcius Conversion If you already have a C++ compiler set up, or if you already have done the steps above, try the following as a challenge! :D Write a C++ Program that takes as input a `float fahrenheit`, computes the corresponding temperature in celcius, and outputs the result in celcius. Recall that the conversion formula is: $$C=(F-32)*5/9.$$ Use this program to tell me what the normal temperature of a human being (98.4F) is, in Celcius. ## Leading Hints
Hint 1 In C++, every variable needs to be _declared_ before using. For example, in python you may be fine if you do ~~~~~~~~py fahrenheit = int(input('Enter temperature in F: ')) celcius = (fahrenheit - 32) * 9 / 5 print('Temperature in Celcius: ', celcius) ~~~~~~~~ But in C++, every variable needs to have a specified type declared before its usage.
Hint 2 You can declare the variables and take the input by doing ~~~~~~~~cpp #include int main() { float fahrenheit, celcius; std::cout<<"Enter temperature in F: "; std::cin>>fahrenheit; // Convert Fahrenheit to Celcius here // Output Celcius return 0; } ~~~~~~~~ Your job is to replace the two comments up there with the correct code.
# Further Exercises Please go through the homework problems for this week that are posted on blackboard: they are gonna help you get used to coding in C++. We will not spend a lot of time setting up and getting used to C++ syntax, so please make sure you're as comfortable as possible before the course accelerates into the data structure part.