## Lesson 8: Hard Debugging

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)

[/spoiler]

[spoiler title=”Lesson Source Code”]

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

int main(){

//9>8 == true == 1, 1>7 ==false

//nested if's
//if inside of an if
//Homework:  If the salesperson made less than 4000 in a month, they do not get any bonus
//Fix all the if statements so that they work properly
//Bonus: If the person made over 15000, they get a 100 bonus added to their check after commision has been factored in.
// ==, <, > < !=, >=, <=, !, &&, ||

double pay = 0.0, comission = 0.0, sales = 0.0;
cout << "Enter the salesperson's sales for the month: ";
cin >> sales;

if (sales > 4000 && sales <5000){
comission = 0.01;
}
else if (sales >5000 && sales < 6000){
comission = 0.02;
}
else if (sales >6000 && sales < 7000){
comission = 0.03;
}
else if (sales >7000 && sales < 8000){
comission = 0.04;
}
else if (sales >8000 && sales < 9000){
comission = 0.05;
}
else{
comission = 0.08;
}

pay = 400;

//cout << "Pay = " << pay << " -- comission = " << comission << "\n\n";
cout << "\nThe salesperson's pay for the month is: " << (pay*(1 + comission)) << endl;

system("PAUSE");
return 0;
}

```

[/spoiler]

Homework: No homework this lesson

## C++ Tutorial #1 — Hello World

Topics covered: During this video I introduce #include (used to include a header file into a program) using namespace std (this will be discussed at length later on) and cout.

Source code:

Hello World
“Hello World” is the quintessential “starter program” in most programming languages, we simply include enough files to write this message to the screen, and then try to explain what each part of the program does. In this case the program will look something like this (I don’t have the source code transcribed for this video right now):

``````
#include <iostream>
using namespace std;
int main(){

cout << "Hello World" << endl;

return 0;
} //end main
``````

Now let’s break the above code down part by part and explain what each part of it does.

#include <iostream>
This line tells our compiler to “include” the library “iostream”, which is also known as input/output stream. This library contains all the code required to use the functions cout / cin (covered later) in our programs. For quite some time cin / cout will be all that we’re really using iostream for, as many of the more advanced uses of it just aren’t sensible at this point.

Using namespace std;
Namespaces can be fairly confusing to some people, because until you see what the above program looks like without this statement it’s hard to understand what exactly it does, so I’ll show you that, and then try to explain it a bit better:

``````
#include <iostream>
int main(){
std::cout<<"Hello World" << endl;

return 0;
}
``````

In the above code you’ll notice that I left out the `using namespace std;` but had to change `cout<<` to `std::cout<<`. This is what the `using namespace std; ` line does. It allows us to use items from the ‘standard namespace’ without mentioning the namespace that they’re in. We’ll talk about namespaces MUCH later on in the series when we get out of using just STD, and maybe get into Boost and other libraries.

int main(){
This line is fairly simple, it shows where the start of the “main” part of our program is. I want you guys to take special notice to the ` { ` though. That’s known as a Scope Bracket, these can make your life easier or harder based on the habits you develop with them now. You guys should use this as something of a rule when it comes to programming: “Every line I write should end with either a `;` or an `{ / }`. This will make your life much easier, especially in the early stages of programming.

cout <<“Hello World”<<endl;
This line calls the function “cout” from the iostream library, and passes in the text string “Hello World”. The part after that (endl) is just to make a new line after that. We’ll get more into how this works in subsequent lessons

return 0;
This line tells the compiler that it can “return” (to the command prompt) if everything went well. The return command will make more sense later. The reason we’re using return 0 and not some other number will be covered way later as well.

} //end main
This is really just a closing bracket ` } `, the ` //end main` after it is what’s known as a comment. Comments are used by programmers to keep track of what’s what in a program. In this case when you have a longer program with many brackets, you might want to know that this bracket is closing your int main() opening bracket.

## C++ Tutorial #2 — Simple variables and cin

Homework: C++ Homework 1: Outputting variables

Topics covered: Simple variable declarations with primitive datatypes, cin (an iostream member)

Source code: Source Code Available Here

Declaring variables
In this lesson I teach you guys how to declare variables and work with ‘built-in types’ within c++. The proper declaration for a variable in C++ is as follows: `Type variableName`. The types that were introduced in this lesson were int, and double. Int is an integer number, while double is a number that can contain a floating decimal value.

## C++ Tutorial #3 — Order of operations, Simple interest, compounding interest

Topics covered: Order of operations in C++, simple interest formula / calculation, compounding interest formula calculation

Source code:

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

int main(){

//PEMDAS
//In math -- Parenthesis, Exponents, Mult / division, add / sub
//In C++ Paren. -- Evaluated first
//Mult / Div
//Add / Sub -- Last in priority
//Evaluates left to right

//1*5-4*3
//5-12
double principal=0, rate=0, time=0;

cout << "What is the initial investment?  ";
cin >> principal;

cout << "What is annual percentage yield (APY -- APR)?  ";
cin >> rate;

cout << "How many years will this be invested for? ";
cin >> time;

cout << "The amount of money at the end of the time period will be: ";

return (0);
}``````

Order of operations
In C++, there is an order of operations, much like the one that we see in traditional mathematics. In C++, the order of operations is as follows:

1. Parenthesis
2. Multiplication and Division
The Cmath library contains a lot of useful things, for instance square root (sqrt) floating point modulo (fmod / fmodf) and more. In this case we are using ‘pow’ which is used like this: `pow(double, int);`