In this tutorial I’ll be covering While, Break, Continue, Exception Handling, Do While, Random Numbers and more complex problems for you to solve.
The problems are starting to get more complex, but don’t worry if you can’t solve them. The goal is just to get better at solving problems, which only comes from practice. All of the code follows the video below.
If you like videos like this, consider donating $1, or simply turn off AdBlocker. Either helps me make more videos.
Cheat Sheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
// ---------- C++ Tutorial 4 ---------- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <numeric> std::vector<int> Range(int start, int max, int step); int main() { // ----- WHILE / BREAK / CONTINUE ----- // While loops continue to run the code in them // as long as a condition is true // Create the variable used in the while condition // before the while loop int i = 1; while (i <= 20){ // If a value is even don't print it if((i % 2) == 0){ i += 1; // Continue skips the rest of the code // and jumps back to the beginning // of the loop continue; } // Break stops execution of the loop and jumps // to the line after the loops closing } if(i == 15) break; std::cout << i << "\n"; // Increment i so the loop eventually ends i += 1; } // ----- END WHILE / BREAK / CONTINUE ----- // ----- PROBLEM DRAW A PINE TREE ----- // For this problem I want you to draw a pine // tree after asking the user for the number // of rows. Here is the sample program /* How tall is the tree : 5 # ### ##### ####### ######### # */ /* I know that this is the number of spaces and hashes for the tree 4 - 1 3 - 3 2 - 5 1 - 7 0 - 9 Spaces before stump = Spaces before top So I need to 1. Decrement spaces by one each time through the loop 2. Increment the hashes by 2 each time through the loop 3. Save spaces to the stump by calculating tree height - 1 4. Decrement from tree height until it equals 0 5. Print spaces and then hashes for each row 6. Print stump spaces and then 1 hash */ std::cout << "How tall is the tree : "; // The top of the tree starts with 1 hash int treeHeight = 0, spaces = 0, hashes = 1, stumpSpaces = 0; std::cin >> treeHeight; // Starting spaces = treeHeight - 1 spaces = treeHeight - 1; // Spaces before stump = tree height - 1 stumpSpaces = treeHeight - 1; // while there are more rows to print keep going while(treeHeight != 0){ // Print spaces before hashes for(auto x: Range(1, spaces, 1)) std::cout << " "; // Print the hashes for(auto x: Range(1, hashes, 1)) std::cout << "#"; // Newline after each row std::cout << "\n"; // Spaces decremented by 1 each time spaces -= 1; // Hashes incremented by 2 each time hashes += 2; // Decrement treeHeight so loop ends treeHeight -= 1; } // Print spaces befor stump for(auto x: Range(1, stumpSpaces, 1)) std::cout << " "; // Print stump std::cout << "#"; return 0; } std::vector<int> Range(int start, int max, int step){ // Every while statement needs an index // to start with int i = start; // Will hold returning vector std::vector<int> range; // Make sure we don't go past max value while(i <= max){ // Add value to the vector range.push_back(i); // Increment the required amount i += step; } return range; } // ----- END PROBLEM DRAW A PINE TREE ----- // ----- EXCEPTION HANDLING EX 1 ----- // Exceptions are errors that occur when things don't // go as expected. // 1. You expect and int and get a string // 2. You expect a file to be available and it isn't // 3. You expect the user to not enter 0 and they do // You try to execute code that could be troublesome // and if an error occurs you catch it and throw // it to another block of code for handling double num1 = 0, num2 = 0; std::cout << "Enter number 1 : "; std::cin >> num1; std::cout << "Enter number 2 : "; std::cin >> num2; try{ if(num2 == 0){ throw "Division by zero is not possible"; } else { printf("%.1f / %.1f = %.2f", num1, num2, (num1 / num2)); } } catch(const char* exp){ std::cout << "Error : " << exp << "\n"; } // ----- END OF EXCEPTION HANDLING EX 1 ----- // ----- EXCEPTION HANDLING EX 2 ----- try { std::cout << "Throwing exception\n"; // Another way to throw an exception throw std::runtime_error("Error Occurred"); // This point is never reached std::cout << "Can you print me?\n"; } // All exceptions are based on std::exception catch(std::exception &exp){ // what() Prints an explanatory string std::cout << "Handled Exception : " << exp.what() << "\n"; } // If exceptions aren't caught any place else // this will catch it catch(...){ std::cout << "Default exception catch\n"; } // ----- END OF EXCEPTION HANDLING EX 2 ----- // ----- DO WHILE LOOPS ----- // Needed for the time function #include <ctime> // Do while loops are guaranteed to execute at least once // We'll create a secret number guessing game // Generate random number from 0 - 10 // We need to seed the random number generator // time() returns the number of seconds since 1, 1, 1970 srand(time(NULL)); // If you divide any number it is impossible to get a // remainder >= 11 int secretNum = std::rand() % 11; int guess = 0; do{ std::cout << "Guess the number : "; std::cin >> guess; if(guess > secretNum) std::cout << "To Big\n"; if(guess < secretNum) std::cout << "To Small\n"; } while(secretNum != guess); std::cout << "You guessed it\n"; // ----- END OF DO WHILE LOOPS ----- // ----- PROBLEM CREATE A DO WHILE LOOP WITH WHILE ----- secretNum = std::rand() % 11; while(true){ std::cout << "Guess the number : "; std::cin >> guess; if(guess > secretNum) std::cout << "To Big\n"; if(guess < secretNum) std::cout << "To Small\n"; if(guess == secretNum) break; } std::cout << "You guessed it\n"; // ----- END PROBLEM CREATE A DO WHILE LOOP WITH WHILE ----- // ---------- End of C++ Tutorial 4 ---------- |
Leave a Reply