/learn-cpp

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

learn-cpp

Basic starter

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void function() {
	cout << "function\n\n";
}

int main() {
	function();
}

#include

use to use some functions like python modules


#include <ofstream>
to write to/create files;
#include <ifstream>
to read from files;
#include <fstream>
to read and write from files;       https://www.w3schools.com/cpp/cpp_files.asp
#include <cmath>
to use some math functions;         https://cplusplus.com/reference/cmath/

Data types

Data Type	Size		Description
boolean		1 byte		Stores true or false values, cout << (10 > 9); cout << (10 == 15); bool = true;
char		1 byte		Stores a single character/letter/number, or ASCII values
int		2 or 4 bytes	Stores whole numbers, without decimals
float		4 bytes		Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
double		8 bytes		Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits


Operator	Name		 	 	Description												 	Example
+			Addition		 	Adds together two values									 	x + y	
-			Subtraction		 	Subtracts one value from another							 	x - y	
*			Multiplication	 	Multiplies two values										 	x * y	
/			Division		 	Divides one value by another								 	x / y	
%			Modulus			 	Returns the division remainder								 	x % y	
++			Increment		 	Increases the value of a variable by 1						 	++x	
--			Decrement		 	Decreases the value of a variable by 1						 	--x
<</>>		Right/Left Shift 	Moves binary to the direction of arrow by amount (*2 or /2) 	x >> y

Operator	Name						Example
==			Equal to					x == y	
!=			Not equal					x != y	
>			Greater than				x > y	
<			Less than					x < y	
>=			Greater than or equal to	x >= y	
<=			Less than or equal to		x <= y

commands

(string).append()
(string).length()
string[number]
const
cout << (message)
cin >> (variable)
if(varibles)
ifstream FileVarThing("filename.txt")
ofstream FileVarThing("filename.txt")

functions/how to excute code

Use, int main() {code}, for the auto excuting code and, void Function name {code}, inside of with parameters(or not) to excute

to use parameters do like:
void dfgh((data type) (name) = (default value),(next parameter) ) {
	cout << (name)
	return (variable);
}

USE:

#include <string>
#include <iostream>
using namespace std;

int main() {
	(code)
	return (variable);
}

character data type

char a = 65, b = 66, c = 67;
cout << a; #A
cout << b; #B
cout << c; #C
	65 is the ASCII value for A

strings

To use strings, you must include an additional header file in the source code, the <string> library:
#include <string>

if()
int x = 1;
if(x=1);
	cout << "works:";

string var1, var2;
cout << "Say a word\n";
cin >> dfgh;
cout << "say another word\n";
cin >> xcvb;
string combined = dfgh + xcvb
cout << combined

+ and append()

string dfgh = "something";
string xcvb = " or another";
string message = dfgh + xcvb; OR string message = dfgh.append(xcvb);
cout << message;
Output: something or another

size()/length()

string dfgh = "something";
string dfghLen = dfgh.length();
cout << dfghLen;
 Output: 9

string[]

string dfgh = "something";
cout << dfgh[4];
dfgh[2] = 's';
cout << dfgh;
 Output: t (\n) sosething 

constants/const

 Makes read only and good if you are unlikly to change the value
const string dfgh = "something";
dfgh[0] = 't';
 Output: Error

Declare muiltiple varibles

string dfgh = "something", xcvb = " or another";
cout << dfgh + xcvb;
 Output: something or another
string dfgh, dfgh1, dfgh2;
dfgh = dfgh1 = dfgh2 = "something";
cout << dfgh << dfgh1 + dfgh2;
 Output: somethingsomethingsomething

read and write files/ofstream and ifstream

#include fstream
 make file
ofstream FileName("filename.txt");

 write to file
FileName << "something or another";

 close file to save memory usage
FileName.close();

 read file
 make varible to store line of file
string readline;
 read file
ifstream FileName("filename.txt")