Quick Reference


Setting up your Compiler/IDE

Using Git

Git setup and usage video

Video walkthru:

Common uses

Add latest changes, commit, and push to server

Use git add . to add all changed files as ready to commit.

Check the list of items that changed with git status.

Use git commit -m "Comment" to make a snapshot of all of your changes currently.

(You can do multiple adds and commits as you're working on something over time.)

You might have updates to your repository. You may need to pull the latest changes to your computer before pushing your own changes. Use git pull here.

Push all the changes to the repository server with git push.

git add .
git status
git commit -m "Thing I did"
git pull
git push

Commands list

Command Description Example
git config --global FIELD Set up your name and email in Git for the first time.
git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"
git clone URL Clones the repository at the URL to your desktop.
Note - this isn't the browser URL from the repository's webpage online, you have to click the "Clone" button and copy the HTTPS link (or, if you have SSH key set up on your machine, the SSH link).
git clone https://gitlab.com/rsingh13/cs235.git
git pull Pulls any latest changes from the repository, attempts to auto-merge with any committed changes on your machine.
Note - Commit your changes before pulling!
git pull
git push Push any committed changes to the server.
Note - Pull latest changes before pushing!
git push
git status View the status of all the files in the repository - what's been changed? What's been added for a commit?
git status
git commit -m "Message" Commit all the added changes to a snapshot. The -m flag lets you add a message at the same time.
git commit -m "Fixed the bug... I think."
git add . -n Preview what would happen if you ran git add .; doesn't actually add anything to the commit.
git add . -n
((list of files shows up)
git add . Add all changed files to prepare to commit. (Recursive)
git add .
git add *.cpp Add only all cpp files. (Recursive)
git add *.cpp
git add FILENAME Adds just one file to prepare to commit.
git add README.md


Setting up SFML (Third party library)

SFML is a third-party library you can use to add graphics, audio, etc. to your C++ programs. We have some optional assignments around using SFML in order to introduce how to link a library into your programs.

The SFML webpage has documentation on how to set it up, but here are some quick rundowns.

SFML-dev.org has the tutorials under Learn -> Tutorials


Style guide

Your code should be clean and easy to read!

Style guide


Common errors

See Quick Search page for error search.


Common instructions

See Quick Search page for instruction search.


C++ Quick Reference

Variables and named constants

Declare a variable:


int myNumber;
string myString;
float myFloat;
Declare a variable and initialize it:


int myNumber = 10;
string myString = "Hello!";
float myFloat = 9.99;
char myChar = 'a';
bool myBool = true;
Declare a named constant it:

const [DATATYPE] [NAME] = [VALUE];

const int NUMBER = 10;
const string STRING = "Hello!";
const float FLOAT = 9.99;
const char CHAR = 'a';
const bool BOOL = true;
Assign a value to a variable that has already been declared:


myNumber = 100;

Console input and output

Output text to the console:

cout << [ITEM];

cout << "Text";
cout << myVariable;
cout << "Label: " << myVariable << endl;
Get input from the keyboard and store it in a variable:


cin >> myInteger;
cin >> myString;
Get a full line of text (with spaces) and store in a variable (string only):

getline( cin, [VARIABLENAME] );

getline( cin, myString );

Note that if you have a cin >> statement immediately before the getline statement, your input will get skipped. Instead, you need to add a cin.ignore() statement:

cin >> myNumber;
getline( cin, myString );


If statement:

If the condition evaluates to true, then execute the code within the if statement. Otherwise, skip that code completely.


if ( a == 1 )
If/Else statement:

If the condition from if statement results to false, then the code in the else case is executed instead. No condition is written with the else case.


if ( a == 1 )
If/Else if statement:

else if ( CONDITION )

if ( a == 1 )
else if ( a == 2 )
If/Else if/Else statement:

else if ( CONDITION )

if ( a == 1 )
else if ( a == 2 )
Switching based on a variable value:

switch( VARIABLE ) {
  case VALUE:

switch( myNumber )
    case 1:
    cout << "It's one!" << endl;
    case 2:
    cout << "It's two!" << endl;
    cout << "I don't know what it is!" << endl;
Looping with While Loops

Runs 0 or more times, depending on whether the CONDITION is true.

while ( CONDITION )

while ( a < b )
    cout << a << endl;
Looping with Do...While Loops

Runs at least once and then continues looping if the condition is true.

} while ( CONDITION );

do {
    cout << "Enter a number: ";
    cin >> input;
} while ( input < 0 || input > max );
Looping with For Loops

Iterate some amount of times through the loop based on your counter variable and range.

  // Do this multiple times

for ( int i = 0; i < 10; i++ )
    cout << i << endl;
Looping with Range-based For Loops

Iterates over all elements in a collection. Only moves forward, doesn't give access to index.

  // Do this multiple times

vector<int> myVec = { 1, 2, 3, 4 };
for ( int element : myVec )
    cout << element << endl;
Declare array (traditional)

When declaring a vanilla array, you need to set its size. You can either hard-code the size with a number:

string arr[10];
Or use a named constant:
string students[TOTAL_STUDENTS];
Or initialize it with a list:
string cats[] = { "Kabe", "Luna", "Pixel", "Korra" };

Declare array (array object)

Remember that you will need #include <array> at the top of your file to use the array object.

Declare an array object, passing in what the data type of it values are, and the size of the array:

array<float, 3> bankBalances;

You can then use the array's size function to get the size of the array.

Iterate over a traditional array

Use a for loop to easily iterate over an array.

const int TOTAL_STUDENTS = 10;
string students[TOTAL_STUDENTS];

for ( int i = 0; i < TOTAL_STUDENTS; i++ )
    cout << "Enter name for student " << i << ": ";
    cin >> students[i];
Iterate over an object array

Use a for loop to easily iterate over an array.

array<string,10> students;

for ( int i = 0; i < students.size(); i++ )
    cout << "Enter name for student " << i << ": ";
    cin >> students[i];
Create an output file and write

ofstream output;
output.open( "file.txt" );

// Write to text file
output << "Hello, world!" << endl;
Create an input file and read

ifstream input;
input.open( "file.txt" );
if ( input.fail() )
  cout << "ERROR: could not load file.txt!" << endl;
string buffer;

// read a word
input >> buffer;

// read a line
getline( input, buffer ); 
Prevent file duplication

When creating .h/.hpp files, it is important to include these lines to prevent file duplication, which happens if you #include this file in more than one place.


// Code goes here

Function declaration

A function declaration contains the function header and no body. It declares that the function exists and will be defined elseware.


void DisplayMenu();
int Sum(int a, int b);
Function definition

A function definition defines how the function operates. It includes the function header and a function body, which has commands in it.


void DisplayMenu()
    cout << "1. Deposit" << endl;
    cout << "2. Withdraw" << endl;
    cout << "3. Quit" << endl;
int Sum(int a, int b)
    return a + b;
Calling a function

Calling a function requires invoking the function's name and passing in any arguments.


int result = Sum( 1, 2 );
int num1 = 2;
int num2 = 5;
int result = Sum( num1, num2 );
Declaring a class
    // Public members
    // Protected members
    // Private members
// Don't forget ; at end of class block
Declare a class function

The class' function declaration goes within the class declaration in the .h/.hpp file:

class Example
    void Setup( int a, float b );
    int     m_a;
    float   m_b;
Define a class function

The class' function definition goes in the .cpp file:

void Example::Setup( int a, float b )
    m_a = a;
    m_b = b;
Getters and Setters

Getter functions:

  • Don't take in any input data (no parameters).
  • Return the value of a private member variable (has a return).
string Player::GetName()
    return m_name;

Setter functions:

  • Take in an input value of the new data to be stored (has a parameter).
  • Doesn't return any data (no return).
void Player::SetName( string newName )
    m_name = newName;
Declare a pointer

DATATYPE* ptrname = nullptr;

int* ptrInt = nullptr;
string* ptrString = nullptr;
Assign an address to a pointer

Pointer variables store addresses as their values. To get the address of a variable, you use the address-of operator &.

ptrname = &variable;

int myInteger = 5;
int* ptrInt = nullptr;
ptrInt = &myInteger;
Dereference a pointer

A pointer stores an address, and dereferencing that pointer gives you the value of the item being pointed to.


cout << *ptrname;
cin >> *ptrname;
Check if pointer is pointing to an address

You will need to use an if statement to check if the pointer is pointing to nullptr or not. nullptr is an invalid address, so we assume the address is valid if it's not nullptr.

if ( ptrname != nullptr )
Allocate memory for a single variable

DATATYPE* ptrname = new DATATYPE;

int * num = new int;
Node* newNode = new Node;
Deallocate memory of a single variable

DELETE ptrname;

delete num;
delete newNode;
Allocate memory for an array

DATATYPE* ptrname = new DATATYPE[ SIZE ];

int * numList = new int[ 100 ];
Node* nodeList = new Node[ 20 ];
Deallocate memory of an array

DELETE [ ] ptrname;

delete [] numList;
delete [] nodeList;