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.]
cd /Users/svisa/Desktop/Lab1CommandLine
g++ -o Lab1 Lab1.cpp
NOTE: if OS cannot find the g++ compiler, type in your Terminal sudo, then try compiling again. Another fix is adding the path to MinGW/bin to the PATH Environment Variable (Microsoft Windows)./* PROJECT: Lab1 assignment on command line compilation FILE: main.cpp PURPOSE: To illustrate the use of command line compilation PROGRAMMER: S. Visa */ #include <iostream> //include the input/output classes using namespace std; //use the standard C++ namespace so names do not have to be fully qualified int main() { cout << "Hello World!\n"; //print Hello World! to the terminal followed by a return cout << "Enter an integer to terminate the program:"; int dummy; cin >> dummy; //type an integer and return to exit the program - this makes the executable "wait" // so we can see the program output
return 0
}
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.
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
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> int main () { |
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.
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; |
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.
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.
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.
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.