## Lesson 16: Case Study- Calculator with Switch statement

[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>
#include <string>

using namespace std;
int main(){
//Switch Statements to build a calculator
int choice = 0;
char operation = ' ';
double firstNum = 0.0, secondNum = 0.0, outcome = 0.0;

cout << "Enter a Value: ";
cin >> firstNum;
cout << "Enter a Second Value: ";
cin >> secondNum;
cout << "Select an option: 1 for +, 2 for -, 3 for *, 4 for /: ";
cin >> choice;

switch (choice){
case 1:{
outcome = firstNum + secondNum;
operation = '+';
break;
}
case 2:{
outcome = firstNum - secondNum;
operation = '-';
break;
}
case 3:{
outcome = firstNum * secondNum;
operation = '*';
break;
}
case 4:{
outcome = firstNum / secondNum;
operation = '/';
break;
}
default:{
cout << "\nYou've ruined EVERYTHING!!!!" << endl;
break;
}
}

//The value of 7 + 12 = 19
cout << "The value of: " << firstNum << " " << operation << " " << secondNum << " = " << outcome;

cout << endl;
system("PAUSE");
return 0;
}
```

[/spoiler]

Homework: None

Switch Statements
This is the first lesson where we touch on switch statements. If you take a look at them, they work almost exactly the same as an if statement. The only major difference between them is the absolute necessity to use break. Otherwise you can use the same bracket structure (though it’s not required in the cases inside of a switch, I prefer it for readability), and the same flow of logic.

Here’s an example of an if and a switch doing the same thing.

```switch(x){
case 1:{
x += 5;
break;
}
case 2:{
x += 10;
break;
}
default:{
x += 1;
break;
}

//Note- The { } brackets can be dropped from these if's since they're 1-liners if you really want.
if( x == 1) { x += 5; }
else if(x == 2){ x += 10; }
else { x += 1; }
}

```

In general I find it easier to avoid switch statements in C++, but some people do feel as though they are used well for menus and things of that nature. It’s also more readable to most people that case is the result of a choice of a value, rather than an arbitrary test (which is what an if portrays). This is the other advantage of switch statements. They are almost always tied to user choices.

## Lesson 17: Data validation / Type checking with StringStream

[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>
#include <string>
#include <sstream> //If you type this and get an error your stuff is too old

using namespace std;
int main(){

string str = "";
int value = 0;
bool cont = true;
while (cont){
cout << "Enter a value: ";
getline(cin, str);
stringstream ss(str);
if (ss >> value && value >5 ){
cont = false;
}
}

cout << endl;
system("PAUSE");
return 0;
}
```

[/spoiler]

Homework: None

What is StringStream?
StringStream is a library that is used to convert from strings to numeric types in C++. StringStream models it’s usage behaviors after iostream so it’s easy to learn for people that are familiar with the basics of C++. StringStream replaces older methods of doing this like atoi or itoa and gives a much safer, error-free method of handling data parsing in C++.

## Lesson 18: Introduction to Functions

[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>
#include <string>

using namespace std;

int addNumbers(int, int); //Function Prototype

int main(){

//Homework -- Create a function that allows 2 variables to be set to it, multiply them together, output the result, return the outcome to main, add 5 and redisplay the outcome.
int a = 5, b = 7;

cout << "The total of " << a << " + " << b << " = " << addNumbers(a, b);

cout << endl;
system("PAUSE");
return 0;
}

//int -- return type -- return goes back to where it was called from
//addNumbers -- Name of our function
// () --Arguments
// (int x, int y) -- argument list
//{  } -- Scoping brackets

int addNumbers(int x, int y){
return x + y;
}
```

[/spoiler]

Homework: Create a function that allows 2 variables to be set to it, multiply them together, output the result, return the outcome to main, add 5 and redisplay the outcome.

What is a function
A function is a section of code that can be called independently from anywhere else in your program to perform a specific task or group of tasks. Functions are able to be passed variables, called “arguments” that are able to be used in the function. These variables are usually passed by “value” (meaning a copy of the variable is made and passed to the function) and in certain cases are passed by reference (meaning the item of the argument itself is passed). Pass by referent will be touched on next lesson, and later on in the series.

Function declarations are broken into 3 parts. The return type, the name, and the argument list.

The return type also dictates another piece of code. If you specify anything other than void (return nothing) as a return type, then you need to return a variable / constant of that type.

For example, the following are all valid examples of returning an int.

```//Assume we need to return an int, and x is an int.
return x;
return x+5
return 5;
return (2*4)*x;
```

A few example functions and how you could call them

```//Example functions
string returnNumberAsWord (int x){
if ( x == 1) return "one";
if ( x == 2) return "two";
if ( x == 3) return "three";
return "Your number was too high for this function"; //This will only get hit if the 3 before it didn't happen
}

char numberToletter( int x){
if (x == 1) return 'a';
if (x == 2) return 'b';
if (x == 3) return 'c';
...
if (x == 26) return 'z';
return ' '; // Will return a space if none of the above match.
}

int addFive( int x ){
return x+5;
}

...

int main(){
string word = returnNumberAsWord(3); // Three
char someLetter = numberToLetter(3); // c
int y = addFive(3); //8

cout << "Word: " << word << "\tsomeLetter: " << someLetter << "\ty:" << y << endl;
system("PAUSE");
return 0;
}
```

## Lesson 19: Functions, return types, pass by value / reference

[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(){
int x = 0;
//Homework
//Using an array of strings for days of the week and an array for days (your choice on type) in the months of the year, ( 31, 28,...)
//Figure out the last day of a user input month for the year 2015, or 2016.
//2016, July etc.

/*cout << addFive(x) << endl; // What will this line output?
addFive(x); //5 that floats and does nothing
cout << x << endl; // What will this line output?

cout << "Reference: " << endl;
cout << addFiveRef(x) << endl; // What will this line output?
cout << x << endl; // What will this line output?*/

int myArray = { 1, 3, 5, 7, 9 };

for (int i = 0; i < 5; i++){
cout << myArray[i] << endl;
}

system("pause");
return 0;
}

y += 5;
return y ;
}

y += 5;
return y;
}

for (int i = 0; i < 5; i++){
anArray[i] += 5;
}
}
```

[/spoiler]

Homework: Using an array of strings for days of the week and an array for days (your choice on type) in the months of the year, ( 31, 28,…) Figure out the last day of a user input month for the year 2015, or 2016. Ex: 2015, July.

What is passing by reference?
Passing by reference is when you pass an argument into a function directly. The default way which you pass arguments into a function is called “Pass by value” in which a perfect copy of the variable you are passing is made, and the one you passed is not affected by any actions taken within the function. Pass by reference passes the variable itself into the function (via reference, which we will touch on later), and any alterations made to that variable throughout the course of the function are passed made directly to the variable itself and thus persist outside of the function even without assignment.

The only difference in syntax between passing by reference and passing by value is an ampersand (&) which is put before the variable name in the function’s argument list.

int addFive(int &myNumber){} // pass by reference
int addFive(int myNumber){} //pass by value

## Lesson 20 — Introduction to Objects (classes) and Constructors

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

[/spoiler]

[spoiler title=”Source Code”]

```#include <iostream>

using namespace std;

//struct
//class

class car{
public:
int windows;
int wheels;
double fuelEconomy; //Miles Per Gallon // Litres per Kilo

void figureMPG(double milesDrivem, double gallonsOfGas){
fuelEconomy = milesDrivem / gallonsOfGas;
}
car::car();
car::car(int, int);
car::car(int, int, double, double);

};

car::car(){
wheels = 4;
windows = 4;
}

car::car(int wheel, int window){
wheels = wheel;
windows = window;
}

car::car(int wheel, int window, double totalMiles, double maxGas){
wheels = wheel;
windows = window;
figureMPG(totalMiles, maxGas);
}

int main(){
car Sonata(6, 8, 350, 18);

cout << "The Hyundai Sonata has " << Sonata.wheels << " wheels " << Sonata.windows << " Windows, and gets " << Sonata.fuelEconomy << " mpg" <<endl;

system("PAUSE");
return 0;
}

}
```

[/spoiler]