Hi all, my name is Jacob and I am here to run through some slightly higher level coding introductions with you.
Just to give you a little background on myself I have a degree in Computer Science and Mathematics and undergraduate in Electrical and Electronic Engineering and Interned in Biostatistics researching genetic mutations in the human eye.
I am currently freelancing and focusing on my own personal coding projects. You might have seen my blogs about my Dragonfruit project.
I am going to be taking you through some of the basics of C++ programming and hopefully getting you to a position where you can start to write your own programs.
The first thing we need to be able to code, is an IDE or an integrated development environment, today we are going to use C++ Shell. This is an online tool, so you don’t need to download anything, and can be found by typing cpp.sh into your browser.
This brings you to this page:
This top section we’re looking at is your workspace, this is where you’ll write your programs.
It comes preloaded with a sample program. So let’s take a minute to familiarise ourselves with the layout.
Below this to the right we have the run button, clicking this will attempt to compile and run what is in the workspace above.
Then right at the bottom you have three windows: your options, which we will be leaving as are; compilation, which will tell you if there is anything wrong with what you have written and execution, which will show you the output of your program.
Now that we have had a look at the page setup, it’s time for a very basic crash course in C++ syntax.
We’re going to break down the code as simply as possible. Line one starts with with a double forward slash ‘//’, this tells both us and the compiler that everything after this point, on this line is a comment – it is meant for a person reading the code, not for the compiler which runs the code. The complier ignores comments.
Lines two and three starting with #include tell us what libraries are referenced in the program, a library is a collection of code that someone else has written to make everyday tasks easier. On line two we have <iostream>, these are your basic input/output streams and allows you to make words appear on the screen or lets you input something that you type directly into the program. Line three is <string> which allows you to build a word or sentence out of characters.
Moving onto line five we have our main function, every program you write will need one of these. It is where the program starts and it will give you an error if there isn’t one. Inside of the main function is the code that powers what the program is actually doing.
What the program is actually doing starts at line seven, this is where a user is going to be able to start interacting with a program. std here stands for standard and refers to the string library that we brought in at the top of the program to declare that we want a string variable called name. A variable is something we want to be able to set, in this case our name, you want your variables to be named clearly and accurately to avoid confusion as your program grows.
Then on line eight we we use cout , (a standard output) to write out to the screen asking “what is your name?” and in line nine we wait for the user to input an answer and then save it into the ‘name’ variable that was set up earlier.
This means that what ever the user has just entered is now “saved” and we can use it by calling on the variable ‘name’.
On line ten the word hello is printed out followed by our variable ‘name’ which now holds whatever was entered and was read in (inputted) as part of line nine.
Line eleven closes the main function and ends the program. If we run this you’ll see what we just went through in action. Click run and give it a second to compile. If you click the compilation window you’ll see it was successful.
In the execution window it will asks your name showing that we’re now sat on line 9 waiting for your input.
So I enter my name Jacob and hit enter to let it know you’ve finished typing, that will tell the program to move onto the next line of code and the message from ten will display.
At the very bottom you’ll see a message to let you know that the program ended normally (by reaching the end of the code).
We have covered a lot of material very quickly here, if you are unsure about anything don’t worry, it is a lot of information and it will all be covered in more depth as we continue. Have a go at editing this program for yourselves to see if you can get it to output a different message.
Your challenge: Have the program say: “Hello, may I take your name please?” and to output “Nice to meet you *your name here*, I am your program!”