Show Menu
Cheatography

Engineering 150 Midterm Cheat Sheet Cheat Sheet by

CPP Cheat Sheet for ENGN 150 Midterm

Basic Operators

+
Addition
-
Subtra­ction
*
Multip­lic­ation
/
Division
=
Assign to Variab­le/Set equal to
%
Modulus (Int repers­ent­ation of remainder)
++var
increase before evaluation
var++
increase after evaluation
condition ? result : altern­­ative;
short form of if-like structure

Input/­Output

Cin
cin >> number;
Cout
cout << "­­Enter an integer: ";

Legal C++ Identi­fiers (The Rules for Var Names)

- Consists of letters (A-Z, Upper and Lowercase)
- Digits (NO DECIMALS)
- Unders­cores
- No Special Characters or Spaces
- No C++ Keywords by itself
- Must be Unique within it's namespace
- Case Sensitive

Example Switch Case

switch (operation) {
	case 0:
		// Things

		break;
	case 1:
		// Things

		break;
	case 2:
                // Things

		break;
        default:
                // Things

                break;
}
Operation must be an Int, Char, or Boolean. Case: must reflect that datatype.

Example If Else

if ( conditional ) {
   // do something
}
else if ( another_conditional ) {
   // do something else
}
else {
   // do something as default
}

Example For Sentence

for ( init; test; command ) {
   // do something
}
"­­br­e­a­k;­­" and "­­co­n­t­in­­ue;­­" identical effects.

Example Do While Sentence

do {
 ­ ­ //do something
} while (bool expres­sion);
 

Boolean Logic

==
Test of Equality
!=
Test of Non-Eq­uality
<
Less Than
>
Greater Than
<=
Less Than or Equal
>=
Greater Than or Equal
!
NOT
&&
AND
||
OR
Boolean expres­­sions in C++ are evaluated left-t­­o-­r­ight!

Data Types

int
3
float
3.0f
double
3.0
char
'a'
string
"­­Hello World!­­"
bool
true/false

File Input / Output

#include <fstream.h>

ifstream file; //read buffer
ofstream file; //write buffer

file.open ("filename", [file mode constant]);

 //Test if the file was created 
if(fs.is_open())     if(fs)

//Reads/Writes like cin and cout
file >> var; //Read
file << ''Text: "<< var << endl; //Write

//Read Entire line
getline (file,String); 

//Read until it arrives at the end of file
while(file.eof())

//Detect if the read/write fail
if(file.fail())

//Close File
file.close();

Procedures

 //Declaration
 void ProcedureName()
{
   // do something
}

//Call to procedure
ProcedureName();
In the procedures we don't receive variables and don't return other variable.

Functions

//Declaration
 [returnType] functionName ( [input1Type input1Name, input2Type input2Name, ....] ) 
{
   // do something
   return value; // value must be of type returnType
}

//Call to function
[returntype var =] functionName ([input1Type input1Name, input2Type input2Name, ....])
We have two methods to create and call functions:
passed with values and passed for reference.

Pass by reference: we put & before variable in the declar­­ation.
ReturnType must be a Datatype
Cannot return more than one thing
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet
          Modern C++ 17 Standard Library Features Cheat Sheet