Sample Project (Mac)
This is a sample project. We'll use it to have you:
- practice downloading starter code
- practice downloading data files
- building and running uses VS Code
- running from the command line
Create a new project
- Find your class folder.
- Double click on the project creation script.
- Enter a project name like: sample_project
- Hit return - the script runs.
- Hit return - the window closes.
Enter the folder just created using the terminal
- Start up a terminal. You are now in your home directory.
- You are instructed to create your class folder in your home folder so it should be there. Also, you just visited it using the Finder. Suppose you called your class folder
csc1810
. Type the following into your terminal and hit returncd csc1810
. - Now, enter the folder containing your freshly made sample project:
cd sample_project
.
Get the starter code
Copy this whole line and paste it into your terminal. Triple click on the line to easily select the whole line.
wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/pkivolowitz/sample_project/master/main.cpp
- The file you just downloaded is called
main.cpp
but you already had amain.cpp
in the folder. Therefore, the new file was automatically renamed tomain.cpp.1
. - Rename
main.cpp.1
tomain.cpp
by entering this:mv main.cpp.1 main.cpp
into the terminal and hit return.
Get the data file
Copy this whole line and paste it into your terminal. Triple click on the line to easily select the whole line.
wget --no-check-certificate --content-disposition https://raw.githubusercontent.com/pkivolowitz/sample_project/master/datafile.txt
Start up VS Code
You can do this two ways:
- Double click in Finder on the
project.code-workspace
file, or - Since you may already be in the terminal, type:
code project.code-workspace
.
main.cpp
Edit In the explorer view on the left, double click on main.cpp
.
The explorer view is the top most icon on the left. It is a toggle. The toggle action can also be performed with command-b.
Build and launch the program
You can do this two ways:
- Click on the insect with the play button icon (fourth one down) then click on the green play button next to the work
run
, or - press
F5
Use the program
- A terminal window should appear - the last thing it prints are the instructions on how to use the program (printed by the program).
- Type in a few lines. They are echoed back to you.
- Hit control-d (^D). This should exit the program and may or may not close the window.
Run the program with the data file
- If the
Terminal
window in Code is open, it will say something aboutpress any key to close it.
If this is so, click in the window and hit any key. Theany
key is shift-6. Just kidding. Any key means ANY key. The whole tab may disappear. - If it does disappear, depress command-j.
- A new terminal appears right inside Code! Alas, it too has the same name as the previous tab even though it does something completely different. This is one example of what we Computer Scientists call: crappy design.
- If you were to execute
ls
in this new terminal you would see:
a.out datafile.txt project.code-workspace
a.out.dSYM main.cpp
a.out
is the name of the executable file (program file) you built. The reason it is calleda.out
is that it is short forassembler output
and was so named by Ken Thompson.- Enter
./a.out
and hit return. - You're now running your program. Use it and exit.
- Let's use the data file. Enter
./a.out < datafile.txt
. - Instead of input coming from you, the input came from the data file. The key here is the
<
. It meansredirect input from
. Your instructors may use this term a lot so please remember it.