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

Undefined reference to WinMain

Your program can't find main(), the main entrypoint to the program.

  1. Make sure you've written your int main() function.
  2. Make sure you're working within a project, and didn't just open the files separately.
    Look at your solution explorer / project explorer - does it list a project and your code files, or is it empty?

Visual Studio: Fatal error LNK1104: cannot open file ...exe

  • Try to close Visual Studio and re-open it.
    Sometimes if your program crashed last time you ran it, Visual Studio will be unable to update the .exe file and give this error.
  • Turn off your virus scan
    Sometimes your virus scan will see the .exe file you just built as an ``unknown threat'' and prevent it from opening, which also causes this error.

Code::Blocks: error: ld returned 1 exit status

  • Make sure you do not have any #include statements that include a .cpp file. Includes only work with .h and .hpp files, or library files in < > brackets.

Windows maximum path length error

Windows has a setting that limits the maximum file path length. If your repository is deep down many folders, you may encounter this issue.

  • Enable long paths
    The Windows documentation(Windows documentation: Maximum Path Length Limitation) has some information on enabling long paths.
  • Move your project directory
    If you move your project directory to something like C:\ instead of the desktop (which is actually C:\Users\blahblahblah\Desktop) or the default visual studio project path (C:\Users\blahblahblah\source\repos) this will help avoid this maximum path length issue.

Code::Blocks: Modern C++ features not working

In this case you'll need to set up your compiler to build for a modern C++ version, rather than the default of C++98 (from 1998).

In Code::Blocks, go to Settings > Compiler...

Then, check the box that is next to the Have g+ follow the C++17 ISO C++ language standard [-std=c++17] option, or the C++14 one if that's not available.

XCode: Cannot find functions

This is a result of creating a new file in XCode, but choosing the wrong file type. When adding .cpp files, make sure you select a C++ file object.

XCode: Where are my output/input text files??

You will need to set up a Working Path for your projects in XCode. You can get to this option by going to the menu bar: Scheme -> Edit Scheme

You will want to set your working directory to a specific path where you can locate the text files.

Git: [rejected] error: failed to push some refs

! [rejected]
error: failed to push some refs
hint: Updates were rejected because the tip fo your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The hint gives you the solution: Run git pull and then repeat your git push.

Git: There is no tracking information for the current branch.

There is no tracking information for the current branch. Please specify which branch you want to merge with.

Try to fix this by running

git pull origin main

Git: fatal: not a git repository (or any of the parent directories): .git

fatal: not a git repository (or any of the parent directories): .git

You are not running your git commands from within a repository folder. Make sure you open Git Bash within a folder, or use cd commands to navigate to your repository path.

Git: error: Browse.VC.opendb Permission denied

error: open("..../Browse.VC.opendb"): Permission denied
error: unable to index file '.../Browse.VC.opendb'
fatal: adding files failed

This happens because Visual Studio is still open. Close Visual Studio before running git add .

Build/run errors

Error message Information
Undefined reference to ___
A variable or function either has not been declared or cannot be found at the current location in the program.

Check to make sure you don't have any typos, that your variables have been declared, that your #include are set up properly, and that any functions declared also are defined elseware.
Memory access violation
If the memory address it shows you is 0x00000 or something with a lot of zeroes, then you're trying to access a pointer that is pointing to nullptr.

Another problem might be going outside of bounds of an array.
Using if statements and it always goes to the wrong condition! If you're comparing if two items are equal, make sure you're using == and not =.


Common instructions

Instruction Explanation Example code
"Increment VARIABLE by 1" Add 1 to the VARIABLE.
"Access the item at position ___" Access an element of the array at the position given.
// Access element of m_arr at position m_count:
m_arr[m_count] = "Bob";

// Access element of m_students at position m_studentCount and call Display():
"Create a basic program loop" Create a loop (usually in main()) that continues looping until the user decides to quit.
bool done = false;
while ( !done )
    cout << "0.   QUIT" << endl;
    cout << "1.   SOMETHING" << endl;
    cout << "2.   SOMETHING" << endl;
    cout << "3.   SOMETHING" << endl;
    int choice;
    cin >> choice;
    if ( choice == 0 )
        done = true;
    // etc.
"Iterate through elements of ARRAY" Use a for loop to iterate over elements of the array, usually starting at 0 and ending at size-1.
string arr[10];
for ( int i = 0; i < 10; i++ )
    arr[i] = "";
Class setter function - "Set the private member variable to the value of the parameter passed in." You're writing a Setter function for a class that has a private member variable. Whatever parameter is passed into this Set function, you're using that as the new value of that private member variable.

Class declaration:

class Example
    void SetVar( string value );
    string m_var;

Setter function:

void Example::SetVar( string value )
    m_var = value;
Class getter function - "Return the value of the corresponding private member variable" You're writing a Getter function for a class that has a private member variable. Return the value of that private member variable.

Class declaration:

class Example
    string GetVar();
    string m_var;

Setter function:

string Example::GetVar()
    return m_var;
Add a file guard to your header files Header files (.h and .hpp) need to have file guards to prevent duplicate imports.

In a header file:

#ifndef _FILE_NAME
#define _FILE_NAME

// Put your code here



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;