Video walkthru:
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
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 |
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
[DATATYPE] [VARIABLENAME];
int myNumber;
string myString;
float myFloat;
[DATATYPE] [VARIABLENAME] = [VALUE];
int myNumber = 10;
string myString = "Hello!";
float myFloat = 9.99;
char myChar = 'a';
bool myBool = true;
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;
[VARIABLENAME] = [VALUE];
myNumber = 100;
cout << [ITEM];
cout << "Text";
cout << myVariable;
cout << "Label: " << myVariable << endl;
cin >> [VARIABLENAME];
cin >> myInteger;
cin >> myString;
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;
cin.ignore();
getline( cin, myString );
If the condition evaluates to true, then execute the code within the if statement. Otherwise, skip that code completely.
if ( CONDITION )
if ( a == 1 )
{
}
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 ( CONDITION )
else
if ( a == 1 )
{
}
else
{
}
if ( CONDITION )
else if ( CONDITION )
if ( a == 1 )
{
}
else if ( a == 2 )
{
}
if ( CONDITION )
else if ( CONDITION )
else
if ( a == 1 )
{
}
else if ( a == 2 )
{
}
else
{
}
switch( VARIABLE ) {
case VALUE:
break;
}
switch( myNumber )
{
case 1:
cout << "It's one!" << endl;
break;
case 2:
cout << "It's two!" << endl;
break;
default:
cout << "I don't know what it is!" << endl;
}
Runs 0 or more times, depending on whether the CONDITION is true.
while ( CONDITION )
{
CODE THAT CONTINUES RUNNING
}
while ( a < b )
{
cout << a << endl;
a++;
}
Runs at least once and then continues looping if the condition is true.
do
{
CODE THAT CONTINUES RUNNING
} while ( CONDITION );
do {
cout << "Enter a number: ";
cin >> input;
} while ( input < 0 || input > max );
Iterate some amount of times through the loop based on your counter variable and range.
for ( INITIALIZATION; CONDITION; UPDATE )
{
// Do this multiple times
}
for ( int i = 0; i < 10; i++ )
{
cout << i << endl;
}
Iterates over all elements in a collection. Only moves forward, doesn't give access to index.
for ( INITIALIZATION : RANGE )
{
// Do this multiple times
}
vector<int> myVec = { 1, 2, 3, 4 };
for ( int element : myVec )
{
cout << element << endl;
}
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:
const int TOTAL_STUDENTS;
string students[TOTAL_STUDENTS];
Or initialize it with a list:
string cats[] = { "Kabe", "Luna", "Pixel", "Korra" };
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.
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];
}
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];
}
ofstream output;
output.open( "file.txt" );
// Write to text file
output << "Hello, world!" << endl;
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 );
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.
#ifndef _FILENAME_HPP
#define _FILENAME_HPP
// Code goes here
#endif
A function declaration contains the function header and no body. It declares that the function exists and will be defined elseware.
RETURNTYPE FUNCTIONNAME( PARAMETERS );
void DisplayMenu();
int Sum(int a, int b);
A function definition defines how the function operates. It includes the function header and a function body, which has commands in it.
RETURNTYPE FUNCTIONNAME( PARAMETERS )
{
}
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 requires invoking the function's name and passing in any arguments.
FUNCTIONAME( ARGS );
DisplayMenu();
int result = Sum( 1, 2 );
int num1 = 2;
int num2 = 5;
int result = Sum( num1, num2 );
class CLASSNAME
{
public:
// Public members
protected:
// Protected members
private:
// Private members
};
// Don't forget ; at end of class block
The class' function declaration goes within the class declaration in the .h/.hpp file:
class Example
{
public:
void Setup( int a, float b );
private:
int m_a;
float m_b;
}
The class' function definition goes in the .cpp file:
void Example::Setup( int a, float b )
{
m_a = a;
m_b = b;
}
Getter functions:
string Player::GetName()
{
return m_name;
}
Setter functions:
void Player::SetName( string newName )
{
m_name = newName;
}
DATATYPE* ptrname = nullptr;
int* ptrInt = nullptr;
string* ptrString = nullptr;
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;
A pointer stores an address, and dereferencing that pointer gives you the value of the item being pointed to.
*ptrname
cout << *ptrname;
cin >> *ptrname;
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 )
DATATYPE* ptrname = new DATATYPE;
int * num = new int;
Node* newNode = new Node;
DELETE ptrname;
delete num;
delete newNode;
DATATYPE* ptrname = new DATATYPE[ SIZE ];
int * numList = new int[ 100 ];
Node* nodeList = new Node[ 20 ];
DELETE [ ] ptrname;
delete [] numList;
delete [] nodeList;