LAB1 (20pts) - Command Line Compilation and the CLion Environment 

Purpose: This lab will familiarize you with compiling C++ programs from the command line and with using the CLion environment.

NOTE: Put your name (as comments) at top of all .cpp and .h files.


PART1: Create and compile your first C++ program using the command line compiler. [The steps below assume you are using MacOS. Differences for Windows are listed where needed.]

Ask questions if you run into problems.


Ask questions if you run into problems. (A possible output sample of your program can be found here.)
Create a snapshot of your output similar to the one shown in the line above. Name it part1.png and submit it inside your zipped folder Lab1CommandLine.zip as expalined at the end of this lab. [Make sure Lab1.cpp and the pictures are in the zip folder.]
Note: 
1) To create a snapshot in Mac OS, click Command+Shift+4, and select the area you want to save as png (is a picture format). You will find your selection saved as a png file on Dektop. 
2) In Windows you may use the print screen key PrtScrn to capture an image of your entire desktop. Then you can paste it in Paint and save it as a png or jpg picture.


PART2: Using CLion.

1. Create a new CLion project.

To open CLion, go to Applications -> CLion, or use the Spotllight search.

To create a new project, click "New project"
Name it (at Location) Lab1CLion
(Leave the type of project as the default "C++ Executable")
Select language standard as C++14
Click "Create" button

2. Hello, World! (output)

In a new project, CLion creates a default C++ file, main.cpp (find it in the left panel), with a main function. When you run a C++ program, it starts executing at the main function. Replace the existing code in main with this equivalent code (copy and paste):

#include <iostream>
using namespace std;

int main () {
    cout << "Hello, World!" << endl;
    return 0;
}

 

Click the Run button (see the gree triangle at the top banner). The program should run, and it should print Hello, World! If not, ask for help.

3. Hello, <your name>! (input)

Now you’ll make the program read in a string of text from the user and print it. Add the following statements at the beginning of the main function (copy and paste):

string name;
cin >> name;

Replace the cout statement with this:

cout << "Hello, " << name << "!" << endl;

Run the program. Does it work?

Add a statement that tells the user to enter her/his name.

4. Count-down function (functions, loops)

Now you’ll add a function to your program that counts from a maximum number down to 0. Add the following code just before the main function:

void countDown(int max) {
   
}

This is a function called countDown. Between the curly braces, write code that prints the integers from max down to 0 (inclusive).

At the beginning of the main function, add the following call to countDown:

countDown(5);

Run the program. Does it work?

Move the countDown function down after the main function. Run the program. Does it work?

Just before the main function, add the following function declaration:

void countDown(int max);

Run the program. Does it work? If not, ask for help.

5. Count-down in a library (functions, .h and .cpp files)

Now you’ll move the countDown function into a pair of files called a library.
Right click on Lab1CLion (located in the left-most panel, above main.cpp) and select New -> C/C++ Header File. Name the new file as count.h.

Move the declaration of countDown (the part before main) into count.h.

Similarly (by using New -> C/C++ Source File), create a file named count.cpp (if necessary, uncheck the box "Create an associated header"). Move from main.cpp the definition of countDown (the part after main) into count.cpp. In count.cpp, somewhere before the countDown function, add the following:

#include "count.h"
#include <iostream>
using namespace std;

In main.cpp, somewhere before the main function, add the following:

#include "count.h"

Run the program. Does it work?

Note: Now inspect the CMakeLists.txt file that CLion created for you; in the add_executable line you will see all the files included in your project. Later in this course you will learn to customize this line so that it includes different files for different runs of the same project. For instance, if in a C++ project you have two files each having a main() functions, the project will not compile; but by editing this line to include only one of the two files, your project will compile and execute. We will practice this knowledge later during class time.

6. Practice problems

For all problems: solve them using the model above (i.e. appropriate entries in count.h and count.cpp) and add code to the main function that allows you to test your functions. (A possible output sample of your program can be found here.) Create a snapshot of your output similar to the one shown. Name it part2.png and submit it as expalined at the end of this lab.

 

Submit your project folder in the following way: