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
Your program can't find main(), the main entrypoint to the program.
int main()
function.#include
statements that include a .cpp file. Includes only work with .h and .hpp files, or library files in < > brackets.Windows has a setting that limits the maximum file path length. If your repository is deep down many folders, you may encounter this issue.
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.
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.
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.
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.
! [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
.
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
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.
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 .
Error message | Information |
---|---|
|
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.
|
|
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 = .
|
Instruction | Explanation | Example code |
---|---|---|
"Increment VARIABLE by 1" | Add 1 to the VARIABLE. |
or
or
|
"Access the item at position ___" | Access an element of the array at the position given. |
|
"Create a basic program loop" |
Create a loop (usually in main() )
that continues looping until the user decides to quit.
|
|
"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. |
|
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:
Setter function:
|
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:
Setter function:
|
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:
|
[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;