|
By Alex, on February 15th, 2021 Because fundamental types have been defined as part of the core C++ language, they are available for immediate use. For example, if we want to define an int variable, we can just do so:
|
int x; // create variable of fundamental type 'int' |
This also works for the compound types that are simple extensions of fundamental types (including pointers, references, and arrays):
|
int *p; // create variable of compound type 'pointer to int' (we'll cover this later in this chapter) |
. . . → Read More: 8.2 — Introduction to program-defined types
By Alex, on December 27th, 2020 You might be surprised to find out that enumerators are actually just named integral constants (and as a result, enumerated types just hold an integral value).
This is similar to the case with chars (%Failed lesson reference, id XX%). Consider:
A char is really just a small integer, and the character ‘A’ gets converted . . . → Read More: 8.3 — Unscoped enumeration input and output
By Alex, on December 5th, 2020 Most programs that have a user interface of some kind need to handle user input. In the programs that you have been writing, you have been using std::cin to ask the user to enter text input. Because text input is so free-form (the user can enter anything), it’s very easy for the user to enter . . . → Read More: 7.16 — std::cin and handling invalid input
By Alex, on December 4th, 2020 In the previous lesson 7.12 — Introduction to testing your code, we discussed how to write and preserve simple tests. In this lesson, we’l ltalk about what kind of tests are useful to write to ensure your code is correct.
Code coverage
The term code coverage is used to describe how . . . → Read More: 7.13 — Code coverage
By Alex, on November 29th, 2020 This lesson continues our exploration of switch statements that we started in the prior lesson 7.4 — Switch statement basics. In the prior lesson, we mentioned that each set of statements underneath a label should end in a break statement or a return statement.
In this lesson, we’ll explore why, and talk about some switch . . . → Read More: 7.5 — Switch fallthrough and scoping
By Alex, on November 23rd, 2020 In lesson %Failed lesson reference, id 7940%, we covered syntax errors, which occur when you write code that is not valid according to the grammar of the C++ language. The compiler will notify of you these type of errors, so they are trivial to catch, and usually straightforward to fix.
We also covered semantic errors, . . . → Read More: 7.14 — Common semantic errors in C++
By Alex, on November 15th, 2020 In a function that takes parameters, the caller may be able pass in arguments that are syntactically valid but semantically meaningless. For example, in the previous lesson (7.15 — Detecting and handling errors, we showed the following sample function:
|
void printDivision(int x, int y) { if (y != 0) std::cout << static_cast<double>(x) / y; else std::cerr << "Error: Could not divide by zero\n"; } |
This function does an explicit check to see if y is 0, since dividing by . . . → Read More: 7.17 — assert and static_assert
By Alex, on November 15th, 2020 So, you’ve written a program, it compiles, and it even appears to work! What now?
Well, it depends. If you’ve written your program to be run once and discarded, then you’re done. In this case, it may not matter that your program doesn’t work for every case — if it works for the one case . . . → Read More: 7.12 — Introduction to testing your code
By Alex, on November 9th, 2020 This lesson is a continuation of lesson 7.2 — If statements and blocks. In this lesson, we’ll take a look at some common problems that occur when using if statements.
Nested if statements and the dangling else problem
It is possible to nest if statements within other if statements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream> int main() { std::cout << "Enter a number: "; int x{}; std::cin >> x; if (x >= 10) // outer if statement // it is bad coding style to nest if statements this way if (x <= 20) // inner if statement std::cout << x << "is between 10 and 20\n"; // which if statement does this else belong to? else std::cout << x << "is greater than 20\n"; return 0; } |
The . . . → Read More: 7.3 — Common if statement problems
By Alex, on November 9th, 2020 The last category of flow control statement we’ll cover is the halt. A halt is a flow control statement that terminates the program. In C++, halts are implemented as functions (rather than keywords), so our halt statements will be function calls.
Let’s take a brief detour, and recap what happens when a program exits normally. . . . → Read More: 7.11 — Halts (exiting your program early)
|
|