C++ Reference Card
C++ Data Types
Data Type Description
bool Boolean (true or false)
char character (‘a’, ‘b’ …)
char[] character array (C-style string if null terminated)
string C++ string (From the STL)
int integer (1,2,-1,1000)
long int long integer
float single precision floating point
double double precision floating point
(These are the most commonly used types)
Operators
++ (post-increment) --(post-decrement)
! (not) ++ (pre-increment) --(pre-decrement)
/ * %(modulus)
+ -
< <= > >=
== !=
&&
||
= *= /= += %= -=
Console input/output
cout<< cout<<”Enter an integer:”; console out, printing to screen
cin >> cin>>i; console in, reading from keyboard
cerr << console error
File Input/Output
Input:
ifstream inputFile;
inputFile.open(“data.txt”);
inputFile >> inputVariable;
//get(char) or getline(entire line) also work
inputFile.close();
output:
ofstream outFIle;
outFIle.open(“output.txt”);
outFile<<outputVariable;
outFile.close();
Decision Statement
If:
if (expression)
Statement;
If / else:
if (expression)
statement;
else:
statement;
switch/case:
switch(int expression)
{
Case int-constant:
Statement(s);
Break;
Case int-constant:
Statement(s);
Break;
default:
statement;
}
Looping
While loop: While(x<100)
while (expression) cout<<x++<<endl;
statement;
do-while loop:
do do
statement; cout<<x++<<endl;
while (expression); while(x<100);
for loop:
for (initialization; test; update)
statement;