SETTINGS
Appearance
Language
About

Settings

Select a category to the left.

Appearance

Theme

Light or dark? Choose how the site looks to you by clicking an image below.

Light Dark

Language

Preferred Language

All content on utk.claranguyen.me is originally in UK English. However, if content exists in your preferred language, it will display as that instead. Feel free to choose that below. This will require a page refresh to take effect.

About

"utk.claranguyen.me" details

Domain Name: claranguyen.me
Site Version: 3.0.1
Last Updated: 2019/08/18
Getting Started
If you missed lab or failed to copy my code from the projector during lab session, the files I typed/utilised during lab sections 1 and 2 are available here: [LINK]
Synopsis
Welcome to CS102! In this class, you will be learning the basics of the C++ Programming Language and how to use it to write basic programs. It is important that you develop the ability to problem solve with programming.
Your First Program
Let's get straight into it with the boring "Hello, World!" example that all programming languages use:
Code (C++)
#include <iostream>

using namespace std;

int main() {
	cout << "Hello, World!" << endl;
}
Run this code in CodeLite (or whatever IDE or compiler you use) and it should print out the following text:
Output
Hello, World!
From the code above, you should now understand that "cout" allows a user to print out text to the console.
Understanding cout
cout is an interesting beast, allowing you to print out things to the console. It doesn't have to just be a fixed string of text, it can also be a number.
Code (C++)
#include <iostream>

using namespace std;

int main() {
	int a = 2;
	cout << "A is " << a << endl;
}
This will print out:
Output
A is 2
Seeing a pattern here? We can print out multiple things with cout just by adding more "<<"!
Reading input with cin
You've had your fun with cout'ing a few things... but now it is time to actually read in text. We have "cin" which accepts input from the console and puts it into our program:
Code (C++)
#include <iostream>

using namespace std;

int main() {
	int a;
	cout << "Enter A: ";
	cin >> a;

	cout << "A is " << a << endl;
}
This program will pause after printing out "Enter A:" and wait for the user to input a value. If the user input "5" and pressed enter, the program's output would look like this:
Output
Enter A: 5
A is 5
Example: Simple addition with two numbers
Let's say you wanted to read in two numbers, let the computer do addition on them, and then print out the result?
Code (C++)
#include <iostream>

using namespace std;

int main() {
	int a, b, c;
	cout << "Enter A: ";
	cin >> a;
	cout << "Enter b: ";
	cin >> b;
	c = a + b;

	cout << "Result is " << c << endl;
}
If you wanted to skip a step and not use "c", you can do this:
Code (C++)
#include <iostream>

using namespace std;

int main() {
	int a, b;
	cout << "Enter A: ";
	cin >> a;
	cout << "Enter b: ";
	cin >> b;

	cout << "Result is " << (a + b) << endl;
}