/robowaifu/ - DIY Robot Wives

Advancing robotics to a point where anime catgrill meidos in tiny miniskirts are a reality.

Server and LynxChan upgrade done. Canary update and a general address soon. -r

Max message length: 6144

Drag files to upload or
click here to select them

Maximum 5 files / Maximum size: 20.00 MB

More

(used to delete files and postings)


Remember, /agdg/ 's Game Jam is still going on. Go Team! :D


C++ programming textbook; PPP2 Chobitsu Board owner 01/16/2023 (Mon) 03:57:21 No.18749
This is /robowaifu/'s official C++ learning textbook thread. It is based directly on Bjarne Stroustrup's college freshman textbook, Programming Principles and Practice Using C++, commonly referred to as PPP2. [1] This textbook thread belongs with this C++ Learning Classroom thread: (>>19777). note: This is a read-only document in essence. If you happen to catch the thread unlocked while it's still under construction, please resist the temptation to reply ITT -- it will get deleted! For now, just reply in /meta please. :^) --- Full program archives (through chapter 11): >file drop 230504 https://anonfiles.com/o8i6j4p7z8/PPP2_v0_1a_7z >PPP2-v0.1a.tar.xz.sha256sum b8a117369432ccaf82b3d1ad2037dd87bd7344bda9d0e9968ebe14be673db47a *PPP2-v0.1a.tar.xz --- mini FAQ: - Help! How do I pass std::cin data into my program when using Coliru? > A) by (here string) redirecting the data back into the program ( <<< ), within quotes. eg; g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out <<< 'robowaifus brighten your day' https://coliru.stacked-crooked.com/a/7def5c6599b373d6 > B) by (here doc) redirecting the data back into the program ( << ), within newline'd delimiters (eg, 'EOF'). eg; g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out << EOF 1 2 3 EOF https://coliru.stacked-crooked.com/a/fe33c133d5306a22 >for further info: https://www.baeldung.com/linux/heredoc-herestring - Can I use multiple files to build my program using Coliru? > Yes you can. > 1. You create the extra files & share them (after first erasing everything from the file's command line), and making note of the resulting internal Coliru name for each file. > 2. Then for the single file containing the program's proper main() function, on it's command line, you add symbolic links to each of these extra files using the internal names noted above. > 3. Thereafter just issue the regular build command according to the usual g++ compiler rules. >Here's a simple working example: (>>21176) --- 1. https://www.stroustrup.com/programming.html >unless otherwise-noted, everything ITT is MIT (Expat) licensed >copyright 2023
Edited last time by Chobitsu on 05/04/2023 (Thu) 13:51:11.
Part I The Basics Chapter 2 Hello, World! 43 (>>18763) 2.2 The classic first program 45 (>>18765) 2.3 Compilation 47 Chapter 3 Objects, Types, and Values 59 (>>18766) 3.1 Input 60 (>>18798) 3.2 Variables 62 (>>18805) 3.3 Input and type 64 (>>18813) 3.4 Operations and operators 66 (>>18854) 3.5 Assignment and initialization 69 (>>18856) 3.5.1 An example: detect repeated words 71 (>>18857) 3.6 Composite assignment operators 73 (>>18858) 3.6.1 An example: find repeated words 73 (>>18859) 3.7 Names 74 (>>18910) 3.8 Types and objects 77 (>>18911) 3.9 Type safety 78 (>>18913) 3.9.1 Safe conversions 79 (>>18915) 3.9.2 Unsafe conversions 80 Chapter 4 Computation 89 (>>18920) 4.3 Expressions 94 (>>18922) 4.3.1 Constant expressions 95 (>>18969) 4.3.2 Operators 97 (>>18970) 4.3.3 Conversions 99 (>>18994) 4.4 Statements 100 (>>19044) 4.4.1.1 if-statements 102 (>>19209) 4.4.1.2 switch-statements 105 (>>19213) 4.4.1.3 Switch technicalities 106 (>>19354) 4.4.2.1 while-statements 109 (>>19394) 4.4.2.2 Blocks 111 (>>19395) 4.4.2.3 for-statements 111 (>>19480) 4.5 Functions 113 (>>19491) 4.5.1 Why bother with functions? 115 (>>19492) 4.5.2 Function declarations 117 (>>19606) 4.6 vector 117 (>>19617) 4.6.1 Traversing a vector 119 (>>19619) 4.6.2 Growing a vector 119 (>>19625) 4.6.3 A numeric example 120 (>>19628) 4.6.4 A text example 123 Chapter 5 Errors 133 (>>20392) 5.3.1 Syntax errors 137 (>>20393) 5.3.2 Type errors 138 (>>20395) 5.3.3 Non-errors 139 (>>20447) 5.4 Link-time errors 139 (>>20448) 5.5 Run-time errors 140 (>>20502) 5.5.1 The caller deals with errors 142 (>>20521) 5.5.2 The callee deals with errors 143 (>>20559) 5.5.3 Error reporting 145 (>>20588) 5.6.1 Bad arguments 147 (>>20599) 5.6.2 Range errors 148 (>>20633) 5.6.3 Bad input 150 (>>20681) 5.6.4 Narrowing errors 153 (>>20694) 5.7 Logic errors 154 (>>20696) 5.9.1 Practical debug advice 159 (>>20713) 5.10 Pre- and post-conditions 163 (>>20717) 5.10.1 Post-conditions 165 Chapter 6 Writing a Program 173 (>>21320) 6.1 A problem 174 (>>21324) 6.3 Back to the calculator! 178 (>>21325) 6.3.1 First attempt 179 (>>21327) 6.3.3 Implementing tokens 183 (>>21343) 6.3.4 Using tokens 185 (>>21366) 6.4 Grammars 188 (>>21429) 6.4.1 A detour: English grammar 193 (>>21439) 6.4.2 Writing a grammar 194 (>>21444) 6.5.2.1 Expressions: first try 197 (>>21615) 6.5.2.2 Expressions: second try 198 (>>21636) 6.5.2.3 Expressions: third time lucky 199 (>>21662) 6.5.3 Terms 200 (>>21665) 6.5.4 Primary expressions 202 (>>21666) 6.6 Trying the first version 203 (>>21668) 6.7 Trying the second version 208 (>>21669) 6.8 Token streams 209 (>>21670) 6.8.1 Implementing Token_stream 211 (>>21671) 6.8.2 Reading tokens 212 (>>21672) 6.8.3 Reading numbers 214 (>>21673) 6.9 Program structure 215 Chapter 7 Completing a Program 221 (>>21683) 7.2 Input and output 222 (>>21684) 7.3 Error handling 224 (>>21685) 7.4 Negative numbers 229 (>>21688) 7.5 Remainder: % 230 (>>21689) 7.6.1 Symbolic constants 232 (>>21690) 7.6.2 Use of functions 234 (>>21691) 7.6.3 Code layout 235 (>>21692) 7.6.4 Commenting 237 (>>21693) 7.7 Recovering from errors 239 (>>21695) 7.8.1 Variables and definitions 242 (>>21696) 7.8.2 Introducing names 247 (>>21697) 7.8.3 Predefined names 250 Chapter 8 Technicalities: Functions, etc. 255 (>>21719) 8.2 Declarations and definitions 257 (>>21721) 8.2.2 Variable and constant declarations 262 (>>21722) 8.2.3 Default initialization 263 (>>21723) 8.3 Header files 264 (>>21725) 8.4 Scope 266 (>>21728) 8.5.1 Declaring arguments and return type 272 (>>21729) 8.5.2 Returning a value 274 (>>21730) 8.5.3 Pass-by-value 275 (>>21731) 8.5.4 Pass-by-const-reference 276 (>>21733) 8.5.5 Pass-by-reference 279 (>>21736) 8.5.6 Pass-by-value vs. pass-by-reference 281 (>>21737) 8.5.7 Argument checking and conversion 284 (>>21738) 8.5.8 Function call implementation 285 (>>21739) 8.5.9 constexpr functions 290 (>>21740) 8.6 Order of evaluation 291 (>>21741) 8.6.1 Expression evaluation 292 (>>21742) 8.6.2 Global initialization 293 (>>21743) 8.7 Namespaces 294 (>>21744) 8.7.1 using declarations and using directives 296 Chapter 9 Technicalities: Classes, etc. 303 (>>21780) 9.2 Classes and members 305 (>>21781) 9.3 Interface and implementation 306 (>>21783) 9.4.1 struct and functions 308 (>>21784) 9.4.2 Member functions and constructors 310 (>>21786) 9.4.3 Keep details private 312 (>>21788) 9.4.4 Defining member functions 314 (>>21790) 9.4.5 Referring to the current object 317 (>>21791) 9.4.6 Reporting errors 317 (>>21792) 9.5 Enumerations 318 (>>21793) 9.5.1 “Plain” enumerations 320 (>>21794) 9.6 Operator overloading 321 (>>21795) 9.7.1 Argument types 324 (>>21797) 9.7.2 Copying 326 (>>21798) 9.7.3 Default constructors 327 (>>21801) 9.7.4 const member functions 330 (>>21802) 9.7.5 Members and “helper functions” 332 (>>21803) 9.8 The Date class 334
Edited last time by Chobitsu on 04/06/2023 (Thu) 22:54:54.
(>>18750) Part II Input and Output Chapter 10 Input and Output Streams 345 (>>21816) 10.4 Opening a file 350 (>>21819) 10.5 Reading and writing a file 352 (>>21820) 10.6 I/O error handling 354 (>>21823) 10.7 Reading a single value 358 (>>21825) 10.7.1 Breaking the problem into manageable parts 359 (>>21828) 10.7.2 Separating dialog from function 362 (>>21829) 10.8 User-defined output operators 363 (>>21830) 10.9 User-defined input operators 365 (>>21831) 10.10 A standard input loop 365 (>>21832) 10.11.1 In-memory representation 368 (>>21833) 10.11.2 Reading structured values 370 (>>21834) 10.11.3 Changing representations 374 Chapter 11 Customizing Input and Output 379 (>>21863) 11.2.1 Integer output 381 (>>21864) 11.2.2 Integer input 383 (>>21865) 11.2.3 Floating-point output 384 (>>21866) 11.2.4 Precision 385 (>>21867) 11.2.5 Fields 387 (>>21868) 11.3.1 File open modes 388 (>>21869) 11.3.2 Binary files 390 (>>21870) 11.3.3 Positioning in files 393 (>>21871) 11.4 String streams 394 (>>21872) 11.5 Line-oriented input 395 (>>21873) 11.6 Character classification 396 (>>21874) 11.7 Using nonstandard separators 398
Edited last time by Chobitsu on 04/10/2023 (Mon) 02:18:29.
>spare reserve
>spare reserve
>spare reserve
>spare reserve
>spare reserve
>spare reserve
>spare reserve
>spare reserve
Open file (51.80 KB 1140x431 PPP2_p45.png)
>"Here is a version of the classic first program. It writes “Hello, World!” to your screen." The basic rite-of-passage for every programmer tbh. --- >p45 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_02/main_p45.cpp && ./a.out Hello, World! --- >p45 example code: https://rentry.org/PPP2_p45 https://coliru.stacked-crooked.com/a/59c8f4911d363bc1
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:36:15.
Open file (26.23 KB 1140x344 PPP2_p48a.png)
Open file (29.54 KB 1140x356 PPP2_p48b.png)
>"You’ll find that compilers are rather picky about syntax. Leaving out any detail of our program, such as an #include file, a semicolon, or a curly brace, will cause errors. Similarly, the compiler has absolutely zero tolerance for spelling mistakes." This is an abbreviated set of examples from this section (but please feel free to try the others). You should get the point well enough, namely: computers are very picky! :^) --- >p48a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_02/main_p48a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:7:3: error: 'cout' was not declared in this scope 7 | cout << "Hello, World!\n"; | ^~~~ >p48b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_02/main_p48b.cpp && ./a.out Hello, World! --- >p48a example code https://rentry.org/PPP2_p48a https://coliru.stacked-crooked.com/a/61c18d23b902c91b >p48b example code https://rentry.org/PPP2_p48b https://coliru.stacked-crooked.com/a/d3d2a460ef465ed0
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:36:42.
Open file (64.28 KB 1140x531 PPP2_p60.png)
>"Using a string variable, we can read a string from input and write it out again like this" Variables are simply named locations in the computer's memory which hold some kind of data. But...you first need to get that data in there somehow, right Anon? --- >p60 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p60.cpp && ./a.out Please enter your first name (followed by 'enter'): Hello, ! --- >p60 example code https://rentry.org/PPP2_p60 https://coliru.stacked-crooked.com/a/0e0a8817dac8fa72
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:37:01.
Open file (89.15 KB 1140x631 PPP2_p63.png)
>"Basically, we can do nothing of interest with a computer without storing data in memory, the way we did it with the input string in the example above." A variable is a named object with a specific type, such as int, string, or double. It also has a set of operators defined for it, such as '+', '==', or '<<'. --- >p63 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p63.cpp && ./a.out 39 3.5 . Annemarie 1 --- >p63 example code https://rentry.org/PPP2_p63 https://coliru.stacked-crooked.com/a/2acc228a586aa683
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:37:41.
Open file (69.99 KB 1140x556 PPP2_p64a.png)
Open file (82.17 KB 1140x556 PPP2_p64b.png)
>"The input operation >> (“get from”) is sensitive to type; that is, it reads according to the type of variable you read into." --- >p64a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p64a.cpp && ./a.out Please enter your first name and age Hello, (age 0) >p64b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p64b.cpp && ./a.out Please enter your first name and age Hello, ??? (age -1) --- >p64a example code https://rentry.org/PPP2_p64a https://coliru.stacked-crooked.com/a/e41bf0f5838087b7 >p64b example code https://rentry.org/PPP2_p64b https://coliru.stacked-crooked.com/a/7d69c0e05f9042a6
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:38:11.
Open file (55.70 KB 1140x506 PPP2_p65.png)
>>18805 >"A string read using >> is (by default) terminated by whitespace; that is, it reads a single word. But sometimes, we want to read more than one word. [] For example, we can read a name consisting of two words like this" --- >p65 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p65.cpp && ./a.out Please enter your first and second names Hello, --- >p65 example code https://rentry.org/PPP2_p65 https://coliru.stacked-crooked.com/a/6537db3a8f817248
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:38:30.
Open file (100.92 KB 1140x806 PPP2_p66.png)
>"In addition to specifying what values can be stored in a variable, the type of a variable determines what operations we can apply to it and what they mean." --- >p66 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p66.cpp && ./a.out Please enter your count and name 32601 32603 Jr. 32599 -100 --- >p66 example code https://rentry.org/PPP2_p66 https://coliru.stacked-crooked.com/a/3fb2c9cd51d504dc
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:38:48.
Open file (77.19 KB 1140x681 PPP2_p67.png)
>>18813 >"The key points here are that there are a lot of useful operators and that their meaning tends to be the same for similar types." --- >p67 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p67.cpp && ./a.out Please enter a floating-point value: n == 0 n + 1 == 1 three times n == 0 twice n == 0 n squared == 0 half of n == 0 square root of n == 0 --- >p67 example code https://rentry.org/PPP2_p67 https://coliru.stacked-crooked.com/a/bac8ffb30be42526 --- >reference/further reading: https://en.cppreference.com/w/cpp/numeric/math/sqrt
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:39:03.
Open file (67.77 KB 1140x556 PPP2_p68a.png)
Open file (82.78 KB 1140x656 PPP2_p68b.png)
>>18815 >"For strings + means concatenation" >"Comparison of strings is particularly useful" --- >p68a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p68a.cpp && ./a.out Please enter your first and second names Hello, >p68b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p68b.cpp && ./a.out Please enter two names that's the same name twice --- >p68a example code https://rentry.org/PPP2_p68a https://coliru.stacked-crooked.com/a/b8bfede64c9d65b7 >p68a example code https://rentry.org/PPP2_p68b https://coliru.stacked-crooked.com/a/7570b01423bcc7db --- >reference/further reading: https://en.cppreference.com/w/cpp/string/basic_string/operator%2B https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:39:27.
Open file (69.07 KB 1140x556 PPP2_p69.png)
>"In many ways, the most interesting operator is assignment, represented as = . It gives a variable a new value." --- >p69 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p69.cpp && ./a.out 3 4 4 9 11 --- >p69 example code https://rentry.org/PPP2_p69 https://coliru.stacked-crooked.com/a/423251937f6fbdb8 --- >reference/further reading: https://en.cppreference.com/w/cpp/language/operator_assignment https://en.cppreference.com/w/cpp/language/initialization
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:39:41.
Open file (86.16 KB 1140x631 PPP2_p70a.png)
Open file (74.71 KB 1140x656 PPP2_p70b.png)
>>18854 >"We can also illustrate assignments using strings" >"These operations are so similar that C++ allows us to use the same notation (the = ) for both" --- >p70a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p70a.cpp && ./a.out alpha beta beta betagamma betadelta >p70a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p70b.cpp && ./a.out 8 9 howdy! G'day --- >p70a example code https://rentry.org/PPP2_p70a https://coliru.stacked-crooked.com/a/10936e18f29a65ff >p70b example code https://rentry.org/PPP2_p70b https://coliru.stacked-crooked.com/a/54953ea16c5e364a
Edited last time by Chobitsu on 01/18/2023 (Wed) 05:40:06.
Open file (77.96 KB 1140x581 PPP2_p71.png)
>"When you think of it, it is obvious that assignment is most useful when you do things many times. We need an assignment when we want to do something again with a different value" previous = current; is the statement to note here (in the context of the while() loop). --- >p71 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p71.cpp && ./a.out Please enter words --- >p71 example code https://rentry.org/PPP2_p71 https://coliru.stacked-crooked.com/a/0bff213731fbce9b --- >reference/further reading: https://en.cppreference.com/w/cpp/language/operator_comparison
Edited last time by Chobitsu on 01/18/2023 (Wed) 06:31:19.
Open file (57.70 KB 1140x731 PPP2_p73a.png)
>"Incrementing a variable (that is, adding 1 to it) is so common in programs that C++ provides a special syntax for it." >"There are many other common ways of changing the value of a variable based on its current value." --- >p73a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p73a.cpp && ./a.out 2 3 8 -8 2 --- >p73a example code https://rentry.org/PPP2_p73a https://coliru.stacked-crooked.com/a/a025c53654d077c3
Edited last time by Chobitsu on 01/18/2023 (Wed) 06:35:42.
Open file (74.72 KB 1140x656 PPP2_p73b.png)
>"Consider the example of detecting repeated adjacent words above. We could improve that by giving an idea of where the repeated word was in the sequence. A simple variation of that idea simply counts the words and outputs the count for the repeated word" Remember Anon, ctrl+d ends typing in an input stream composed of strings. --- >p73b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p73b.cpp && ./a.out Please enter words --- >p73b example code https://rentry.org/PPP2_p73b https://coliru.stacked-crooked.com/a/fc74b888c313413c
Edited last time by Chobitsu on 01/18/2023 (Wed) 07:33:53.
Open file (77.02 KB 1140x681 PPP2_p75a.png)
Open file (24.63 KB 1140x344 PPP2_p75b.png)
>"In a C++ program, a name starts with a letter and contains only letters, digits, and underscores." >"Names are case sensitive; that is, uppercase and lowercase letters are distinct, so x and X are different names. This little program has at least four errors" --- >p75a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p75a.cpp && ./a.out 1 1 1 1 1 >p75b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p75b.cpp && ./a.out main.cpp: In function 'int Main()': main.cpp:6:3: error: 'STRING' was not declared in this scope 6 | STRING s = "Goodbye, cruel world! "; | ^~~~~~ main.cpp:7:3: error: 'cOut' was not declared in this scope 7 | cOut << S << '\n'; | ^~~~ main.cpp:7:11: error: 'S' was not declared in this scope 7 | cOut << S << '\n'; | ^ main.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type] 8 | } --- >p75a example code https://rentry.org/PPP2_p75a https://coliru.stacked-crooked.com/a/55685ec719badd19 >p75b example code https://rentry.org/PPP2_p75b https://coliru.stacked-crooked.com/a/7f28fe28aaf306b9 --- >reference/further reading: https://en.cppreference.com/w/cpp/language/identifiers
Edited last time by Chobitsu on 01/18/2023 (Wed) 08:41:23.
Open file (93.04 KB 1140x831 PPP2_p76.png)
>>18859 >"The C++ language reserves many (about 85) names as “keywords.” We list them in §A.3.1. You can’t use those to name your variables, types, functions, etc." >"When you choose names for your variables, functions, types, etc., choose meaningful names; that is, choose names that will help people understand your program." --- >p76 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p76.cpp && ./a.out 7 7 7 7 7 7 7 7 7 --- >p76 example code https://rentry.org/PPP2_p76 https://coliru.stacked-crooked.com/a/68a80f7b86b05b2b
Open file (84.57 KB 1140x941 PPP2_p77.png)
>"The notion of type is central to C++ and most other programming languages." >"Informally, we think of an object as a box into which we can put values of a given type." --- >p76 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p77.cpp && ./a.out 7 9 a 1.2 Hello, World! 1.2 --- >p76 example code https://rentry.org/PPP2_p77 https://coliru.stacked-crooked.com/a/f5bd5a237572ef15
Open file (68.28 KB 1140x591 PPP2_p78.png)
>"Every object is given a type when it is defined. A program — or a part of a program — is type-safe when objects are used only according to the rules for their type." --- >p78 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p78.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:9:10: warning: unused variable 'y' [-Wunused-variable] 9 | double y = x; // the value of y is undefined | ^ main.cpp:10:10: warning: unused variable 'z' [-Wunused-variable] 10 | double z = 2.0 + x; // the meaning of + and the value of z are undefined | ^ main.cpp:9:10: warning: 'x' is used uninitialized [-Wuninitialized] 9 | double y = x; // the value of y is undefined | ^ main.cpp:6:10: note: 'x' was declared here 6 | double x; // we “forgot” to initialize: | ^ --- >p78 example code https://rentry.org/PPP2_p78 https://coliru.stacked-crooked.com/a/1a5a6ed70cfde407
Edited last time by Chobitsu on 01/21/2023 (Sat) 06:35:56.
Open file (90.93 KB 1140x1041 PPP2_p79.png)
>"When needed, a char is converted to an int and an int is converted to a double." >"The most useful conversion is int to double because it allows us to mix ints and doubles in expressions" --- >p79 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p79.cpp && ./a.out x 120 x 120 2.3 4.3 --- >p79 example code https://rentry.org/PPP2_p79 https://coliru.stacked-crooked.com/a/35e69f0a3e4c3624
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:01:45.
Open file (79.72 KB 1140x788 PPP2_p80.png)
>"Safe conversions are usually a boon to the programmer and simplify writing code. Unfortunately, C++ also allows for (implicit) unsafe conversions. By unsafe, we mean that a value can be implicitly turned into a value of another type that does not equal the original value." --- >p80 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p80.cpp && ./a.out oops!: 20000 != 32 --- >p80 example code https://rentry.org/PPP2_p80 https://coliru.stacked-crooked.com/a/b84450420557f5a5
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:01:31.
Open file (111.91 KB 1140x963 PPP2_p81.png)
>>18915 >"The problem is that an int is typically much larger than a char, so that it can (and in this case does) hold an int value that cannot be represented as a char. Try it to see what value b gets on your machine (32 is a common result); better still, experiment" --- >p81 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p81.cpp && ./a.out Please enter numbers --- >p81 example code https://rentry.org/PPP2_p81 https://coliru.stacked-crooked.com/a/947eddfdcf90abae
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:01:18.
Open file (78.08 KB 1140x913 PPP2_p82.png)
>>18916 >"They are unsafe in the sense that the value stored might differ from the value assigned. Why can this be a problem? Because often we don’t suspect that an unsafe conversion is taking place." --- >p82 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p82.cpp && ./a.out NOTE: JSON encoding for the output failed due to invalid UTF8. --- >p82 example code https://rentry.org/PPP2_p82 https://coliru.stacked-crooked.com/a/08096f80d81515f2
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:01:03.
Open file (113.92 KB 1140x1037 PPP2_p94.png)
>"The most basic building block of programs is an expression. An expression computes a value from a number of operands." --- >p94 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p94.cpp && ./a.out 20 40 800 99 40 3960 --- >p94 example code https://rentry.org/PPP2_p94 https://coliru.stacked-crooked.com/a/b8c1fff9ac5ca91e
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:00:48.
Open file (112.81 KB 1140x987 PPP2_p95.png)
>>18920 >"We can make more complicated expressions by combining expressions using operators, such as + and *, in just the way that we are used to. When needed, we can use parentheses to group expressions:" --- >p95 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p95.cpp && ./a.out 99 40 3960 278 --- >p95 example code https://rentry.org/PPP2_p95 https://coliru.stacked-crooked.com/a/976c68aaf836cee8
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:00:35.
Open file (111.29 KB 1140x962 PPP2_p96a.png)
>"C++ offers the notion of a symbolic constant, that is, a named object to which you can’t give a new value after it has been initialized." --- >p96a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p96a.cpp && ./a.out 3.14159 2 12.5664 --- >p96a example code https://rentry.org/PPP2_p96a https://coliru.stacked-crooked.com/a/5e249645b408da96
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:00:22.
Open file (99.91 KB 1140x912 PPP2_p96b.png)
>>18922 >"In some places, such as case labels (§4.4.1.3), C++ requires a constant expression, that is, an expression with an integer value composed exclusively of constants." --- >p96b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p96b.cpp && ./a.out 17 19 19 21 --- >p96b example code https://rentry.org/PPP2_p96b https://coliru.stacked-crooked.com/a/5e249645b408da96
Edited last time by Chobitsu on 01/26/2023 (Thu) 09:00:10.
Open file (100.73 KB 1140x962 PPP2_p96c.png)
>>18938 >"A constexpr symbolic constant must be given a value that is known at compile time." --- >p96c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p96c.cpp && ./a.out 100 42 107 --- >p96c example code https://rentry.org/PPP2_p96c https://coliru.stacked-crooked.com/a/4a7fc4cf28d4baf8
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:59:57.
Open file (106.29 KB 1140x1037 PPP2_p97.png)
>>18939 >"To handle cases where the value of a “variable” that is initialized with a value that is not known at compile time but never changes after initialization, C++ offers a second form of constant (a const)" --- >p97 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p97.cpp && ./a.out 100 42 107 49 --- >p97 example code https://rentry.org/PPP2_p97 https://coliru.stacked-crooked.com/a/e3f3830ec1726d33
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:59:43.
Open file (68.69 KB 1140x937 PPP2_p98.png)
>"An increment can be expressed in at least three ways" --- >p98 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p98.cpp && ./a.out 10 11 12 13 --- >p98 example code https://rentry.org/PPP2_p98 https://coliru.stacked-crooked.com/a/2e18ad073fba6a68
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:59:28.
Open file (78.67 KB 1140x862 PPP2_p99a.png)
>"The rule (for the types we have presented so far) is that if an operator has an operand of type double, we use floating-point arithmetic yielding a double result; otherwise, we use integer arithmetic yielding an int result." --- >p99a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p99a.cpp && ./a.out 2 1.25 98 --- >p99a example code https://rentry.org/PPP2_p99a https://coliru.stacked-crooked.com/a/dbe748ac03036975
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:59:15.
Open file (86.27 KB 1140x987 PPP2_p99b.png)
>>18970 >"Once the result has been calculated, the compiler may have to convert it (again) to use it as an initializer or the right hand of an assignment." --- >p99b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p99b.cpp && ./a.out 2.5 2 1.25 1 --- >p99b example code https://rentry.org/PPP2_p99b https://coliru.stacked-crooked.com/a/ec6378ed52f8a863
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:59:00.
Open file (81.85 KB 1140x912 PPP2_p100a.png)
>>18971 >"Beware that it is easy to forget about integer division in an expression that also contains floating-point operands." --- >p100a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p100a.cpp && ./a.out Please enter a temperature centigrade: 32 --- >p100a example code https://rentry.org/PPP2_p100a https://coliru.stacked-crooked.com/a/ca450ec75d54e93f
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:58:47.
Open file (84.33 KB 1140x912 PPP2_p100b.png)
>>18991 >"To get the code mathematically correct, either 9 or 5 (or both) will have to be changed into a double." --- >p100b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p100b.cpp && ./a.out Please enter a temperature centigrade: 32 --- >p100b example code https://rentry.org/PPP2_p100b https://coliru.stacked-crooked.com/a/5ad53a662bdbf615
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:58:33.
Open file (76.05 KB 1140x912 PPP2_p100c.png)
>"So far, we have seen two kinds of statements: expression statements and declarations. An expression statement is simply an expression followed by a semicolon." --- >p100c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p100c.cpp && ./a.out 2 3 --- >p100c example code https://rentry.org/PPP2_p100c https://coliru.stacked-crooked.com/a/f5ecf0292dbd4f9d
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:58:21.
Open file (74.62 KB 1140x812 PPP2_p101a.png)
>>18994 >"When statements follow each other, the computer executes them in the order in which they are written." >"In general, we want a statement to have some effect. Statements without effect are typically useless." --- >p101a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p101a.cpp && ./a.out 7 --- >p101a example code https://rentry.org/PPP2_p101a https://coliru.stacked-crooked.com/a/d80b2f2716dc8d93
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:58:06.
Open file (111.24 KB 1140x1212 PPP2_p101b.png)
>>19042 >"In other words, the if-statement doesn’t matter; y is going to get the value 3 regardless. This is a common error for novice programmers, and it can be difficult to spot, so watch out for it." --- >p101b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p101b.cpp && ./a.out 4 3 5 3 --- >p101b example code https://rentry.org/PPP2_p101b https://coliru.stacked-crooked.com/a/62e18b56ef1cad00
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:57:53.
Open file (96.10 KB 1140x937 PPP2_p102a.png)
>"The simplest form of selection is an if-statement, which selects between two alternatives." >"If its condition is true, the first statement is executed; otherwise, the second statement is. This notion is simple." --- >p102a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p102a.cpp && ./a.out Please enter two integers max(0,0) is 0 --- >p102a example code https://rentry.org/PPP2_p102a https://coliru.stacked-crooked.com/a/2de0ed01f4807fbc
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:57:38.
Open file (109.16 KB 1140x1387 PPP2_p102b.png)
>>19044 >"For example, you were probably told in kindergarten that to cross the street at a traffic light, you had to wait for the light to turn green: “If the traffic light is green, go” and “If the traffic light is red, wait.”" --- >p102b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p102b.cpp && ./a.out go wait --- >p102b example code https://rentry.org/PPP2_p102b https://coliru.stacked-crooked.com/a/d946869567d63dba
Edited last time by Chobitsu on 01/26/2023 (Thu) 08:57:21.
Open file (118.98 KB 1140x987 PPP2_p103a.png)
>>19045 >"Consider what’s wrong with this program" >"The snag is that we didn’t test for bad input. The program assumes that the user enters proper input." --- >p103a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p103a.cpp && ./a.out Please enter a length followed by a separate unit (c or i): 1cm == 0.393701in --- >p103a example code https://rentry.org/PPP2_p103a https://coliru.stacked-crooked.com/a/d946869567d63dba
Edited last time by Chobitsu on 01/28/2023 (Sat) 13:08:15.
Open file (136.16 KB 1140x1062 PPP2_p103b.png)
>>19187 >"We must always test our programs with “bad” input, because someone will eventually — intentionally or accidentally — enter bad input. A program should behave sensibly even if its users don’t." --- >p103b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p103b.cpp && ./a.out Please enter a length followed by a separate unit (c or i): Sorry, I don't know a unit called ' ' --- >p103b example code https://rentry.org/PPP2_p103b https://coliru.stacked-crooked.com/a/eb7b1867fedfe11f
Edited last time by Chobitsu on 01/28/2023 (Sat) 13:08:33.
Open file (119.13 KB 1140x1112 PPP2_p105.png)
>"Actually, the comparison of unit to 'i' and to 'c' is an example of the most common form of selection: a selection based on comparison of a value against several constants. Such selection is so common that C++ provides a special statement for it: the switch-statement." --- >p105 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p105.cpp && ./a.out Please enter a length followed by a separate unit (c or i): Sorry, I don't know a unit called 'a' --- >p105 example code https://rentry.org/PPP2_p105 https://coliru.stacked-crooked.com/a/d34612ea142a90c7
Open file (97.46 KB 1140x1037 PPP2_p106.png)
>"1. The value on which we switch must be of an integer, char, or enumeration (§9.5) type. In particular, you cannot switch on a string. >2. The values in the case labels must be constant expressions (§4.3.1). In particular, you cannot use a variable in a case label. >3. You cannot use the same value for two case labels. >4. You can use several case labels for a single case. >5. Don’t forget to end each case with a break. Unfortunately, the compiler probably won’t warn you if you forget." --- >p106 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p106.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:17:11: error: switch quantity not an integer 17 | switch (s) { // error: the value must be of integer, char, or enum type | ^ --- >p106 example code https://rentry.org/PPP2_p106 https://coliru.stacked-crooked.com/a/34877add4e01bcfa
Edited last time by Chobitsu on 01/28/2023 (Sat) 16:35:10.
Open file (119.51 KB 1140x1337 PPP2_p107a.png)
>>19213 >"A switch-statement generates optimized code for comparing against a set of constants. For larger sets of constants, this typically yields more efficient code than a collection of if-statements. However, this means that the case label values must be constants and distinct." --- >p107a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p107a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:24:10: error: the value of 'y' is not usable in a constant expression 24 | case y: // error: variable in case label | ^ main.cpp:13:18: note: 'int y' is not const 13 | int y = 'y'; // this is going to cause trouble | ^ main.cpp:24:10: error: the value of 'y' is not usable in a constant expression 24 | case y: // error: variable in case label | ^ main.cpp:13:18: note: 'int y' is not const 13 | int y = 'y'; // this is going to cause trouble | ^ main.cpp:30:5: error: duplicate case value 30 | case 'n': // error: duplicate case label (n’s value is ‘n’) | ^~~~ main.cpp:21:5: note: previously used here 21 | case n: | ^~~~ --- >p107a example code https://rentry.org/PPP2_p107a https://coliru.stacked-crooked.com/a/813ea02d272bf2ef
Open file (109.05 KB 1140x1262 PPP2_p107b.png)
>>19255 >"Often you want the same action for a set of values in a switch. It would be tedious to repeat the action so you can label a single action by a set of case labels." --- >p107b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p107b.cpp && ./a.out Please enter a digit is not a digit --- >p107b example code https://rentry.org/PPP2_p107b https://coliru.stacked-crooked.com/a/68e462e8a9296fa8
Open file (114.68 KB 1140x1012 PPP2_p108.png)
>>19277 >"The most common error with switch-statements is to forget to terminate a case with a break." >"Unfortunately, the compiler will accept this, and when you have finished case 'i' you’ll just “drop through” into case 'c' " --- >p108 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p108.cpp && ./a.out Please enter a length followed by a separate unit (c or i): --- >p108 example code https://rentry.org/PPP2_p108 https://coliru.stacked-crooked.com/a/d4cc54c9affa11f0
Edited last time by Chobitsu on 01/29/2023 (Sun) 18:54:33.
Open file (96.33 KB 1140x987 PPP2_p109.png)
>"As an example of iteration, consider the first program ever to run on a stored-program computer (the EDSAC)." >"[It will] calculate and print a simple list of squares" --- >p109 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p109.cpp && ./a.out 0 0 1 1 2 4 3 9 ... 97 9409 98 9604 99 9801 --- >p109 example code https://rentry.org/PPP2_p109 https://coliru.stacked-crooked.com/a/238ba83913557d28
Open file (105.23 KB 1140x1337 PPP2_p111.png)
>"A sequence of statements delimited by curly braces { and } is called a block or a compound statement. A block is a kind of statement." >"The empty block { } is sometimes useful for expressing that nothing is to be done." --- >p111 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p111.cpp && ./a.out 0 0 1 1 2 4 3 9 ... 97 9409 98 9604 99 9801 0 1 --- >p111 example code https://rentry.org/PPP2_p111 https://coliru.stacked-crooked.com/a/8f6b4b1736724580
Open file (110.16 KB 1140x1137 PPP2_p112a.png)
>"Iterating over a sequence of numbers is so common that C++, like most other programming languages, has a special syntax for it. A for-statement is like a while-statement except that the management of the control variable is concentrated at the top where it is easy to see and understand." --- >p112a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p112a.cpp && ./a.out 0 0 1 1 2 4 3 9 ... 97 9409 98 9604 99 9801 0 0 1 1 2 4 3 9 ... 97 9409 98 9604 99 9801 --- >p112a example code https://rentry.org/PPP2_p112a https://coliru.stacked-crooked.com/a/989a42064bbdb6f6
Open file (85.37 KB 1140x887 PPP2_p112b.png)
>>19395 >"[U]sing a for-statement yields more easily understood and more maintainable code whenever a loop can be defined as a for-statement with a simple initializer, condition, and increment operation. Use a while-statement only when that’s not the case." >"Never modify the loop variable inside the body of a for-statement. That would violate every reader’s reasonable assumption about what a loop is doing." You'll see instead how to properly do a 'by-twos' for loop in the next post; in case that's what you actually wanted, Anon. --- >p112b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p112b.cpp && ./a.out 0 0 2 4 4 16 6 36 ... 94 8836 96 9216 98 9604 --- >p112b example code https://rentry.org/PPP2_p112b https://coliru.stacked-crooked.com/a/74b8d74e145d3f93
Edited last time by Chobitsu on 02/02/2023 (Thu) 01:19:27.
Open file (83.25 KB 1140x862 PPP2_p113a.png)
>>19396 >"If you want to increment by 2, say so" >"Please note that the cleaner, more explicit version is shorter than the messy one. That’s typical." --- >p113a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p113a.cpp && ./a.out 0 0 2 4 4 16 6 36 ... 94 8836 96 9216 98 9604 --- >p113a example code https://rentry.org/PPP2_p113a https://coliru.stacked-crooked.com/a/e671476b46997bdf
Open file (75.53 KB 1140x837 PPP2_p113b.png)
>"In the program above, what was square(i)? It is a call of a function. In particular, it is a call of the function called square with the argument i. A function is a named sequence of statements. A function can return a result (also called a return value)." >"The first line of this definition tells us that this is a function (that’s what the parentheses mean), that it is called square, that it takes an int argument (here, called x), and that it returns an int (the type of the result always comes first in a function declaration)" --- >p113b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p113b.cpp && ./a.out 4 100 --- >p113b example code https://rentry.org/PPP2_p113b https://coliru.stacked-crooked.com/a/66ff7556f4b021a3
Edited last time by Chobitsu on 02/04/2023 (Sat) 00:14:02.
Open file (96.47 KB 1140x812 PPP2_p114.png)
>>19480 >"We don’t have to use the result of a function call, but we do have to give a function exactly the arguments it requires." >"You might think that a computer should be smart enough to figure out that by the string "two" you really meant the integer 2." >"However, a C++ compiler deliberately isn’t that smart. It is the compiler’s job to do exactly what you tell it to do after verifying that your code is well formed according to the definition of C++." --- >p114 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p114.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:12:18: error: too few arguments to function 'int square(int)' 12 | int v1 = square(); // error: argument missing | ~~~~~~^~ main.cpp:5:5: note: declared here 5 | int square(int x) // return the square of x | ^~~~~~ main.cpp:13:12: error: invalid conversion from 'int (*)(int)' to 'int' [-fpermissive] 13 | int v2 = square; // error: parentheses missing | ^~~~~~ | | | int (*)(int) main.cpp:14:18: error: too many arguments to function 'int square(int)' 14 | int v3 = square(1, 2); // error: too many arguments | ~~~~~~^~~~~~ main.cpp:5:5: note: declared here 5 | int square(int x) // return the square of x | ^~~~~~ main.cpp:15:19: error: invalid conversion from 'const char*' to 'int' [-fpermissive] 15 | int v4 = square("two"); // error: wrong type of argument – int expected | ^~~~~ | | | const char* main.cpp:5:16: note: initializing argument 1 of 'int square(int)' 5 | int square(int x) // return the square of x | ~~~~^ main.cpp:12:7: warning: unused variable 'v1' [-Wunused-variable] 12 | int v1 = square(); // error: argument missing | ^~ main.cpp:13:7: warning: unused variable 'v2' [-Wunused-variable] 13 | int v2 = square; // error: parentheses missing | ^~ main.cpp:14:7: warning: unused variable 'v3' [-Wunused-variable] 14 | int v3 = square(1, 2); // error: too many arguments | ^~ main.cpp:15:7: warning: unused variable 'v4' [-Wunused-variable] 15 | int v4 = square("two"); // error: wrong type of argument – int expected | ^~ --- >p114 example code https://rentry.org/PPP2_p114 https://coliru.stacked-crooked.com/a/dbf15fbf1030dd6d
Open file (69.28 KB 1140x812 PPP2_p115.png)
>>19481 >"The list of arguments required by the function is called a parameter list and its elements are called parameters (or formal arguments). The list of parameters can be empty, and if we don’t want to return a result we give void (meaning “nothing”) as the return type." --- >p115 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p115.cpp && ./a.out Sorry --- >p115 example code https://rentry.org/PPP2_p115 https://coliru.stacked-crooked.com/a/8106dad15357049e
Open file (71.99 KB 1140x812 PPP2_p116.png)
>"We define a function when we want a separate computation with a name because doing so >• Makes the computation logically separate >• Makes the program text clearer (by naming the computation) >• Makes it possible to use the function in more than one place in our program >• Eases testing" >"Why didn’t we use the version using print_square()? That version is not significantly simpler than the version using square(), and note that >• print_square() is a rather specialized function that we could not expect to be able to use later, whereas square() is an obvious candidate for other uses >• square() hardly requires documentation, whereas print_square() obviously needs explanation" --- >p116 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p116.cpp && ./a.out 0 0 1 1 2 4 3 9 ... 97 9409 98 9604 99 9801 --- >p116 example code https://rentry.org/PPP2_p116 https://coliru.stacked-crooked.com/a/a6f4a1df1ae50ae8
Open file (109.57 KB 1140x1037 PPP2_p117.png)
>"We don’t really need to look at the function body. In real programs, we most often don’t want to look at a function body." >"Fortunately, C++ provides a way of supplying that information separate from the complete function definition. It is called a function declaration" >"So, if you just want to use a function, you simply write — or more commonly #include — its declaration. The function definition can be elsewhere." --- >p117 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p117.cpp && ./a.out 1936 --- >p117 example code https://rentry.org/PPP2_p117 https://coliru.stacked-crooked.com/a/0ddffe4e9580180b
Edited last time by Chobitsu on 02/04/2023 (Sat) 05:54:33.
Open file (110.03 KB 1140x1137 PPP2_p118a.png)
>"To do just about anything of interest in a program, we need a collection of data to work on." >"For example, we might need a list of phone numbers, a list of members of a football team, a list of courses, a list of books read over the last year, a catalog of songs for download, a set of payment options for a car, a list of the weather forecasts for the next week, a list of prices for a camera in different web stores, etc." >"Here we will start with one of the simplest, and arguably the most useful, ways of storing data: a vector." >"A vector is simply a sequence of elements that you can access by an index." --- >p118a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p118a.cpp && ./a.out 5 7 9 4 6 8 Kant Plato Hume Kierkegaard --- >p118a example code https://rentry.org/PPP2_p118a https://coliru.stacked-crooked.com/a/70e74a6b95384cdb
Open file (106.26 KB 1140x937 PPP2_p118b.png)
>>19606 >"Naturally, a vector will only accept elements of its declared element type:" --- >p118b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p118b.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:20:21: error: invalid conversion from 'const char*' to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-fpermissive] 20 | v[2] = "Hume"; // error: trying to assign a string to an int | ^~~~~~ | | | const char* --- >p118b example code https://rentry.org/PPP2_p118b https://coliru.stacked-crooked.com/a/bb3b980150272962
Open file (103.43 KB 1140x1087 PPP2_p118c.png)
>>19609 >"We can also define a vector of a given size without specifying the element values." >"In that case, we use the (n) notation where n is the number of elements, and the elements are given a default value according to the element type." --- >p118c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p118c.cpp && ./a.out 0 0 0 0 0 0 "" "" "" "" --- >p118c example code https://rentry.org/PPP2_p118c https://coliru.stacked-crooked.com/a/a1491a3374bfb7af
Open file (69.11 KB 1140x737 PPP2_p119a.png)
>>19614 >"Please note that you cannot simply refer to a nonexistent element of a vector" >"We will discuss run-time errors and subscripting in the next chapter." --- >p119a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p119a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:12:13: warning: array subscript 20000 is outside array bounds of 'int [6]' [-Warray-bounds] 12 | vi[20000] = 44; // run-time error In file included from /usr/local/include/c++/12.1.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33, from /usr/local/include/c++/12.1.0/bits/allocator.h:46, from /usr/local/include/c++/12.1.0/vector:61, from main.cpp:5: In member function '_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = int]', inlined from 'constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = int]' at /usr/local/include/c++/12.1.0/bits/allocator.h:183:40, inlined from 'static constexpr _Tp* std::allocator_traits<std::allocator<_Up> >::allocate(allocator_type&, size_type) [with _Tp = int]' at /usr/local/include/c++/12.1.0/bits/alloc_traits.h:464:28, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:378:33, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:375:7, inlined from 'constexpr void std::_Vector_base<_Tp, _Alloc>::_M_create_storage(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:395:44, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:332:26, inlined from 'constexpr std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:552:47, inlined from 'int main()' at main.cpp:10:19: /usr/local/include/c++/12.1.0/bits/new_allocator.h:137:55: note: at offset 80000 into object of size 24 allocated by 'operator new' 137 | return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp))); | ^ --- >p119a example code https://rentry.org/PPP2_p119a https://coliru.stacked-crooked.com/a/b9b0ec78e7d45648
Open file (76.24 KB 1140x787 PPP2_p119b.png)
>"A vector “knows” its size, so we can print the elements of a vector like this" >"The call v.size() gives the number of elements of the vector called v. In general, v.size() gives us the ability to access elements of a vector without accidentally referring to an element outside the vector’s range." >"The range for a vector v is [0:v.size()). That’s the mathematical notation for a half-open sequence of elements." --- >p119b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p119b.cpp && ./a.out 5 7 9 4 6 8 --- >p119b example code https://rentry.org/PPP2_p119b https://coliru.stacked-crooked.com/a/a9b2a8bb366e22d7
Open file (75.42 KB 1140x787 PPP2_p119c.png)
>>19617 >"The language takes advantage of the notion of a half-open sequence to provide a simple loop over all the elements of a sequence, such as the elements of a vector." >"This is called a range-for-loop because the word range is often used to mean the same as “sequence of elements.” We read for (int x : v) as “for each int x in v” and the meaning of the loop is exactly like the equivalent loop over the subscripts [0:v.size())." >tl;dr Use range-for-loops instead, whenever you're able to Anon! :^) --- >p119c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p119c.cpp && ./a.out 5 7 9 4 6 8 --- >p119c example code https://rentry.org/PPP2_p119c https://coliru.stacked-crooked.com/a/01c4e58fd42f9976
Open file (132.05 KB 1140x1112 PPP2_p120.png)
>"Often, we start a vector empty and grow it to its desired size as we read or compute the data we want in it. The key operation here is push_back(), which adds a new element to a vector. The new element becomes the last element of the vector." --- >p120 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p120.cpp && ./a.out 2.7 5.6 7.9 --- >p120 example code https://rentry.org/PPP2_p120 https://coliru.stacked-crooked.com/a/6d84240e374bcf3b
Edited last time by Chobitsu on 02/08/2023 (Wed) 01:35:55.
Open file (114.11 KB 1140x1087 PPP2_p121.png)
>"Often, we have a series of values that we want to read into our program so that we can do something with them. The “something” could be producing a graph of the values, calculating the mean and median, finding the largest element, sorting them, combining them with other data, searching for “interesting” values, comparing them to other data, etc." >"There is no limit to the range of computations we might perform on data, but first we need to get it into our computer’s memory." >"As a concrete example, we chose to read in floating-point numbers representing temperatures" --- >p121 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p121.cpp && ./a.out Please enter temperatures: --- >p121 example code https://rentry.org/PPP2_p121 https://coliru.stacked-crooked.com/a/6ba0936a9226bfd0
Edited last time by Chobitsu on 02/08/2023 (Wed) 01:59:22.
Open file (95.13 KB 1140x962 PPP2_p122a.png)
>>19621 >"To limit the scope of our input variable, temp, to the loop, we used a for-statement, rather than a while-statement" --- >p122a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p122a.cpp && ./a.out Please enter temperatures: --- >p122a example code https://rentry.org/PPP2_p122a https://coliru.stacked-crooked.com/a/ed1134f8abfb6ff7
Edited last time by Chobitsu on 02/08/2023 (Wed) 02:31:02.
Open file (152.39 KB 1140x1337 PPP2_p122b.png)
>>19625 >"Once we get data into a vector we can easily manipulate it. As an example, let’s calculate the mean and median temperatures:" --- >p122b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p122b.cpp && ./a.out Please enter temperatures: Average temperature: 0 Median temperature: 0 --- >p122b example code https://rentry.org/PPP2_p122b https://coliru.stacked-crooked.com/a/4bfcdb62497290b2
Edited last time by Chobitsu on 02/08/2023 (Wed) 02:33:43.
Open file (153.64 KB 1140x1262 PPP2_p123.png)
>"From a programmer’s point of view, what’s interesting about this example is its generality: the vector and the simple operations on it can be used in a huge range of applications. It is fair to say that whatever you are interested in, if you need to analyze data, you’ll use vector (or a similar data structure; see Chapter 21)." >"As an example, let’s build a simple dictionary" Stop. Try to understand this example in detail before moving on through the textbook, Anon. If you can do so, then it's fair to say you've completely understood this chapter. It wraps up most every technique used so far into one short program! :^) --- >p123 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p123.cpp && ./a.out Please enter words: Number of words: 0 --- >p123 example code https://rentry.org/PPP2_p123 https://coliru.stacked-crooked.com/a/0d777141f1114798 >le epin 1337 h4x04z version: https://coliru.stacked-crooked.com/a/7ebb4d7ade022842 :^)
Edited last time by Chobitsu on 02/11/2023 (Sat) 10:20:19.
Open file (112.22 KB 1140x1037 PPP2_p137.png)
>"Each of those lines has a syntax error; that is, they are not well formed according to the C++ grammar, so the compiler will reject them." >"Unfortunately, syntax errors are not always easy to report in a way that you, the programmer, find easy to understand." >"That’s because the compiler may have to read a bit further than the error to be sure that there really is an error." >tl;dr Error messages can sometimes be really difficult to discern what's going on. I recommend going directly to the top of the list of messages if you have a long listing. That's usually the most important message to pay attention to, Anon (YMMV). --- >p137 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p137.cpp && ./a.out main.cpp:15:17: warning: character constant too long for its type 15 | int s4 = area('7); // error: non-terminated character ( ' missing) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp: In function 'int main()': main.cpp:12:18: error: expected ')' before ';' token 12 | int s1 = area(7; // error: ) missing | ~ ^ | ) main.cpp:13:16: error: too few arguments to function 'int area(int, int)' 13 | int s2 = area(7) // error: ; missing | ~~~~^~~ main.cpp:9:5: note: declared here 9 | int area(int length, int width); // calculate area of a rectangle | ^~~~ main.cpp:15:60: error: expected ')' before 'missing' 15 | int s4 = area('7); // error: non-terminated character ( ' missing) | ~ ^~~~~~~~ | ) main.cpp:15:16: error: too few arguments to function 'int area(int, int)' 15 | int s4 = area('7); // error: non-terminated character ( ' missing) | ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:9:5: note: declared here 9 | int area(int length, int width); // calculate area of a rectangle | ^~~~ main.cpp:12:7: warning: unused variable 's1' [-Wunused-variable] 12 | int s1 = area(7; // error: ) missing | ^~ main.cpp:13:7: warning: unused variable 's2' [-Wunused-variable] 13 | int s2 = area(7) // error: ; missing | ^~ main.cpp:15:7: warning: unused variable 's4' [-Wunused-variable] 15 | int s4 = area('7); // error: non-terminated character ( ' missing) | ^~ --- >p137 example code https://rentry.org/PPP2_p137 https://coliru.stacked-crooked.com/a/8b33ba99077c9ca2
Edited last time by Chobitsu on 02/19/2023 (Sun) 05:23:38.
Open file (108.97 KB 1140x987 PPP2_p138.png)
>"Once you have removed syntax errors, the compiler will start reporting type errors; that is, it will report mismatches between the types you declared (or forgot to declare) for your variables, functions, etc. and the types of values or expressions you assign to them, pass as function arguments, etc." --- >p138 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p138.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:12:12: error: 'arena' was not declared in this scope; did you mean 'area'? 12 | int x0 = arena(7); // error: undeclared function | ^~~~~ | area main.cpp:13:16: error: too few arguments to function 'int area(int, int)' 13 | int x1 = area(7); // error: wrong number of arguments | ~~~~^~~ main.cpp:9:5: note: declared here 9 | int area(int length, int width); // calculate area of a rectangle | ^~~~ main.cpp:14:17: error: invalid conversion from 'const char*' to 'int' [-fpermissive] 14 | int x2 = area("seven", 2); // error: 1st argument has a wrong type | ^~~~~~~ | | | const char* main.cpp:9:14: note: initializing argument 1 of 'int area(int, int)' 9 | int area(int length, int width); // calculate area of a rectangle | ~~~~^~~~~~ --- >p138 example code https://rentry.org/PPP2_p138 https://coliru.stacked-crooked.com/a/7836d7ca764e8f41
Open file (113.09 KB 1140x987 PPP2_p139.png)
>"As you work with the compiler, you’ll wish that it was smart enough to figure out what you meant; that is, you’d like some of the errors it reports not to be errors. That’s natural." >"More surprisingly, as you gain experience, you’ll begin to wish that the compiler would reject more code, rather than less." >"As you gain experience, you’ll learn how to get the most out of the compiler’s ability to detect errors and to dodge its known weaknesses." --- >p139 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p139.cpp && ./a.out NOTE: JSON encoding for the output failed due to invalid UTF8. --- >p139 example code https://rentry.org/PPP2_p139 https://coliru.stacked-crooked.com/a/4e208c39fb870e87
Open file (150.57 KB 1140x1537 PPP2_p140.png)
>"A program consists of several separately compiled parts, called translation units. Every function in a program must be declared with exactly the same type in every translation unit in which it is used. We use header files to ensure that; see §8.3." >"Every function must also be defined exactly once in a program. If either of these rules is violated, the linker will give an error." --- >p140 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p140.cpp && ./a.out 6 --- >p140 example code https://rentry.org/PPP2_p140 https://coliru.stacked-crooked.com/a/bd60a2f17048e65e
Edited last time by Chobitsu on 02/20/2023 (Mon) 11:59:51.
Open file (129.74 KB 1140x1337 PPP2_p141.png)
>"If your program has no compile-time errors and no link-time errors, it’ll run." >"This is where the fun really starts." >"look at the calculation of the ratio in the code []. It looks innocent enough. Did you notice something wrong with it?" --- >p141 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p141.cpp && ./a.out -1 2 4 -2 -2 0 -inf --- >p141 example code https://rentry.org/PPP2_p141 https://coliru.stacked-crooked.com/a/126ae3d0f145a9d7
Open file (141.90 KB 1140x1362 PPP2_p142a.png)
>"So, let’s tackle the problem of argument errors with area(). We have two obvious alternatives: >a. Let the caller of area() deal with bad arguments. >b. Let area() (the called function) deal with bad arguments." >"Let’s try the first alternative (“Let the user beware!”) first. That’s the one we’d have to choose if area() was a function in a library where we couldn’t modify it. For better or worse, this is the most common approach." --- >p142a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p142a.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive x bash: line 7: 28398 Aborted (core dumped) ./a.out --- >p142a example code https://rentry.org/PPP2_p142a https://coliru.stacked-crooked.com/a/e647b0adbcd448bb
Open file (140.74 KB 1140x1312 PPP2_p142b.png)
>>20502 >"If we didn’t need separate error messages about each argument, we would simplify" --- >p142b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p142b.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive area() argument bash: line 7: 30987 Aborted (core dumped) ./a.out --- >p142b example code https://rentry.org/PPP2_p142b https://coliru.stacked-crooked.com/a/750f8a83b22cd37f
Edited last time by Chobitsu on 02/21/2023 (Tue) 04:41:52.
Open file (159.68 KB 1140x1462 PPP2_p142c.png)
>>20504 >"To complete protecting area() from bad arguments, we have to deal with the calls through framed_area()." >"This is messy, but there is also something fundamentally wrong. We could write this only by knowing exactly how framed_area() used area()." >"We shouldn’t have to know such details!" --- >p142c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p142c.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive 2nd area() argument called by framed_area() bash: line 7: 308 Aborted (core dumped) ./a.out --- >p142c example code https://rentry.org/PPP2_p142c https://coliru.stacked-crooked.com/a/a978fafa9fd5fe5b
Open file (169.37 KB 1140x1487 PPP2_p143a.png)
>>20505 >"We could make the code less brittle by giving the value subtracted by framed_area() a name. That name [can also] be used by code calling framed_area()" >"Look at that code! Are you sure it is correct? Do you find it pretty? Is it easy to read? Actually, we find it ugly (and therefore error-prone). We have more than trebled the size of the code and exposed an implementation detail of framed_area()." >"There has to be a better way!" --- >p143a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p143a.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive argument for area() called by framed_area() bash: line 7: 32136 Aborted (core dumped) ./a.out --- >p143a example code https://rentry.org/PPP2_p143a https://coliru.stacked-crooked.com/a/2e05cbb6e7c08d5b
Edited last time by Chobitsu on 02/25/2023 (Sat) 07:47:20.
Open file (166.75 KB 1140x1462 PPP2_p143b.png)
>"Checking for valid arguments within framed_area() is easy, and error() can still be used to report a problem:" >"if anything to do with the error handling changes, we only have to modify the code in one place." >"One benefit of [this] approach is that the argument-checking code is in one place. We don’t have to search the whole program for calls. Furthermore, that one place is exactly where the arguments are to be used, so all the information we need is easily available for us to do the check." --- >p143b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p143b.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive area() argument called by framed_area() bash: line 7: 17344 Aborted (core dumped) ./a.out --- >p143b example code https://rentry.org/PPP2_p143b https://coliru.stacked-crooked.com/a/5278b381370f893f
Edited last time by Chobitsu on 02/22/2023 (Wed) 07:17:04.
Open file (171.29 KB 1140x1637 PPP2_p145.png)
>"Let’s consider a slightly different question: Once you have checked a set of arguments and found an error, what should you do? Sometimes you can return an “error value.”" >"[This] way, we can have the called function do the detailed checking, while letting each caller handle the error as desired." --- >p145 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p145.cpp && ./a.out Are all x of y?? (yes or no) b 0 0 -1 --- >p145 example code https://rentry.org/PPP2_p145 https://coliru.stacked-crooked.com/a/e0e714ca69e53f91
Open file (182.40 KB 1140x1837 PPP2_p146.png)
>>20559 >"The [case of a caller forgetting to test] can easily lead to surprises." >"Do you see the errors? This kind of error is hard to find because there is no obvious “wrong code” to look at: the error is the absence of [tests in the caller-function f() ]." --- >p146 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p146.cpp && ./a.out terminate called after throwing an instance of 'std::runtime_error' what(): non-positive area bash: line 7: 23929 Aborted (core dumped) ./a.out --- >p146 example code https://rentry.org/PPP2_p146 https://coliru.stacked-crooked.com/a/657385f3e23664c6
Edited last time by Chobitsu on 02/22/2023 (Wed) 09:57:08.
Open file (152.84 KB 1140x1487 PPP2_p147.png)
>"C++ provides a mechanism to help deal with errors: exceptions. The fundamental idea is to separate detection of an error (which should be done in a called function) from the handling of an error (which should be done in the calling function) while ensuring that a detected error cannot be ignored" >"Nothing makes error handling easy, but exceptions make it easier." >"Here is a version of area() using exceptions" >"if the arguments are OK, we return the area as always; if not, we get out of area() using the throw, hoping that some catch will provide an appropriate response." --- >p147 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p147.cpp && ./a.out Oops! bad arguments to area() --- >p147 example code https://rentry.org/PPP2_p147 https://coliru.stacked-crooked.com/a/22a54fef98e27d13
Edited last time by Chobitsu on 02/22/2023 (Wed) 12:50:12.
Open file (95.77 KB 1140x912 PPP2_p148.png)
>"The most common and useful standard library container is the vector we introduced in §4.6. A vector holds a number of elements, and we can determine that number by calling the vector’s size() member function." >"What happens if we try to use an element with an index (subscript) that isn’t in the valid range [0:v.size())?" --- >p148 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p148.cpp && ./a.out Please enter integers: (enter '|' to stop): bash: line 7: 1002 Segmentation fault (core dumped) ./a.out --- >p148 example code https://rentry.org/PPP2_p148 https://coliru.stacked-crooked.com/a/e69b44b0d2368c2a
Open file (71.30 KB 1140x812 PPP2_p149a.png)
>>20599 >"Here is a simpler version that produces the same [type of] range error as the loop" --- >p149a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p149a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:13:22: warning: array subscript 5 is outside array bounds of 'int [5]' [-Warray-bounds] 13 | int x = v[5]; | ^ In file included from /usr/local/include/c++/12.1.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33, from /usr/local/include/c++/12.1.0/bits/allocator.h:46, from /usr/local/include/c++/12.1.0/string:41, from /usr/local/include/c++/12.1.0/bits/locale_classes.h:40, from /usr/local/include/c++/12.1.0/bits/ios_base.h:41, from /usr/local/include/c++/12.1.0/ios:42, from /usr/local/include/c++/12.1.0/ostream:38, from /usr/local/include/c++/12.1.0/iostream:39, from main.cpp:5: In member function '_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = int]', inlined from 'constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = int]' at /usr/local/include/c++/12.1.0/bits/allocator.h:183:40, inlined from 'static constexpr _Tp* std::allocator_traits<std::allocator<_CharT> >::allocate(allocator_type&, size_type) [with _Tp = int]' at /usr/local/include/c++/12.1.0/bits/alloc_traits.h:464:28, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:378:33, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:375:7, inlined from 'constexpr void std::_Vector_base<_Tp, _Alloc>::_M_create_storage(std::size_t) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:395:44, inlined from 'constexpr std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:332:26, inlined from 'constexpr std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/12.1.0/bits/stl_vector.h:552:47, inlined from 'int main()' at main.cpp:12:18: /usr/local/include/c++/12.1.0/bits/new_allocator.h:137:55: note: at offset 20 into object of size 20 allocated by 'operator new' 137 | return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp))); | ^ 0 --- >p149a example code https://rentry.org/PPP2_p149a https://coliru.stacked-crooked.com/a/808ae10aafad386d
Open file (152.86 KB 1140x1387 PPP2_p149b.png)
>>20628 >if the off-by-one code above had been part of a program that caught exceptions, we would at least have gotten a decent error message" >"Note that a range error is really a special case of the argument errors we discussed in §5.5.2." --- >p149b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p149b.cpp && ./a.out Please enter integers: (press '|' to stop): Oops! Range error --- >p149b example code https://rentry.org/PPP2_p149b https://coliru.stacked-crooked.com/a/09a4691fbca9c8de
Open file (132.84 KB 1140x1337 PPP2_p150.png)
>"We can test if the last input operation succeeded by testing cin" >"During the early stages of development, we often want to indicate that we have found an error but aren’t yet ready to do anything particularly clever about it; we just want to report the error and terminate the program." >"Later, maybe, we’ll come back and do something more appropriate." --- >p150 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p150.cpp && ./a.out Please enter a float: Exception: runtime error, couldn't read a double in 'some_function()' --- >p150 example code https://rentry.org/PPP2_p150 https://coliru.stacked-crooked.com/a/4706d0fd7580b7ad
Open file (95.38 KB 1140x962 PPP2_p151.png)
>>20633 >"The standard library defines a few types of exceptions, such as the out_of_range thrown by vector. It also supplies runtime_error which is pretty ideal for our needs because it holds a string that can be used by an error handler." >"When we want to deal with runtime_error we simply catch it. For simple programs, catching runtime_error in main() is ideal" >"The call e.what() extracts the error message from the runtime_error." --- >p151 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p151.cpp && ./a.out --- >p151 example code https://rentry.org/PPP2_p151 https://coliru.stacked-crooked.com/a/7e229dde5940715f
Open file (111.17 KB 1140x1087 PPP2_p152a.png)
>>20635 >"As it happens, out_of_range is not a runtime_error, so catching runtime_error does not deal with the out_of_range errors that we might get from misuse of vectors and other standard library container types." >"However, both out_of_range and runtime_ error are “exceptions,” so we can catch exception to deal with both" >"We added catch(...) to handle exceptions of any type whatsoever." --- >p152a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p152a.cpp && ./a.out --- >p152a example code https://rentry.org/PPP2_p152a https://coliru.stacked-crooked.com/a/f2c1eeef1e452d20
Open file (117.31 KB 1140x1137 PPP2_p152b.png)
>>20668 >"When you use error(), you’ll often wish to pass two pieces of information along to describe the problem. In that case, just concatenate the strings describing those two pieces of information. This is so common that we provide a second version of error() for that" --- >p152b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p152b.cpp && ./a.out --- >p152b example code https://rentry.org/PPP2_p152b https://coliru.stacked-crooked.com/a/8389ea9a1bb2edc0
Open file (71.91 KB 1140x787 PPP2_p153a.png)
>"In §3.9.2 we saw a nasty kind of error: when we assign a value that’s “too large to fit” to a variable, it is implicitly truncated." >"Here x will get the value 2 rather than 2.9, because x is an int and ints don’t have values that are fractions of an integer, just whole integers (obviously). Similarly, if we use the common ASCII character set, c will get the value 42 (representing the character *), rather than 1066, because there is no char with the value 1066 in that character set." --- >p153a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p153a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:11:12: warning: overflow in conversion from 'int' to 'char' changes value from '1066' to ''*'' [-Woverflow] 11 | char c = 1066; | ^~~~ 2 * --- >p153a example code https://rentry.org/PPP2_p153a https://coliru.stacked-crooked.com/a/cc4575cb9c2eed73
Open file (179.12 KB 1140x1587 PPP2_p153b.png)
>>20681 >"In §3.9.2 we saw how we could protect ourselves against such narrowing by testing. Given exceptions (and templates; see §19.3) we can write a function that tests and throws a runtime_error exception if an assignment or initialization would lead to a changed value." --- >p153b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p153b.cpp && ./a.out error: info loss 2.900000 -> 2 --- >p153b example code https://rentry.org/PPP2_p153b https://coliru.stacked-crooked.com/a/66617d63dfca1f03
Open file (139.12 KB 1140x1262 PPP2_p154.png)
>"Logic errors are usually the most difficult to find and eliminate, because [after successfully building,] the computer does [just] what you asked it to." >"Your job now is to figure out why that wasn’t really what you meant." >"Basically, a computer is a very fast moron. It does exactly what you tell it to do, and that can be most humbling." --- >p154 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p154.cpp && ./a.out Please enter temperatures: (enter '|' to stop): High temperature: 0 Low temperature: 0 Average temperature: -nan --- >p154 example code https://rentry.org/PPP2_p154 https://coliru.stacked-crooked.com/a/e76ebab53bdefc92
Open file (138.46 KB 1140x1162 PPP2_p156.png)
>>20694 >"Unfortunately, there are [] errors in [that] program. What would happen if all of the temperatures were below zero? The initialization for high_temp has the equivalent problem to low_temp: high_temp will remain at 0.0 unless there is a higher temperature in the data. This program wouldn’t work for the South Pole in winter either." >"These errors are fairly typical; they will not cause any errors when you compile the program or cause wrong results for “reasonable” inputs." >"However, we forgot to think about what we should consider “reasonable”. Here is an improved program" --- >p156 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p156.cpp && ./a.out Please enter temperatures: (enter '|' to stop): High temperature: -1000 Low temperature: 1000 Average temperature: -nan --- >p156 example code https://rentry.org/PPP2_p156 https://coliru.stacked-crooked.com/a/e436ab6b69b9e1ab
Edited last time by Chobitsu on 02/24/2023 (Fri) 12:43:20.
Open file (118.23 KB 1140x1187 PPP2_p161.png)
>"Occasionally, you will feel that the rules the compiler enforces are stupid and unnecessary (they rarely are) and that things could and ought to be simpler (indeed, but they are not)." >"However, as they say, “a poor craftsman curses his tools.” A good craftsman knows the strengths and weaknesses of his tools and adjusts his work accordingly." >"Here are some common compile-time errors" --- >p161 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p161.cpp && ./a.out --- >p161 example code https://rentry.org/PPP2_p161 https://coliru.stacked-crooked.com/a/f812cd1db7942123
Open file (101.98 KB 1140x962 PPP2_p162a.png)
>>20696 >"When looking for a bug, carefully follow the code statement by statement from the last point that you are sure it was correct. Pretend you’re the computer executing the program. Does the output match your expectations? Of course not, or you wouldn’t be debugging." >"Often, when you don’t see the problem, the reason is that you “see” what you expect to see rather than what you [actually] wrote." --- >p162a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p162a.cpp && ./a.out --- >p162a example code https://rentry.org/PPP2_p162a https://coliru.stacked-crooked.com/a/4a4f523688cfe4ca
Open file (83.03 KB 1140x887 PPP2_p162b.png)
>>20710 >"Eventually, you’ll learn to use [sophisticated debugging] facilities, but for simple problems and simple programs, you can just temporarily put in a few extra output statements (using cerr) to help you see what’s going on." --- >p162b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p162b.cpp && ./a.out my_fct(1,2.2) my_fct() returns 0 --- >p162b example code https://rentry.org/PPP2_p162b https://coliru.stacked-crooked.com/a/6c4de5acaa797625
Open file (148.09 KB 1140x1487 PPP2_p163.png)
>"Insert statements that check invariants (that is, conditions that should always hold; see §9.4.3) in sections of code suspected of harboring bugs." >"A statement that states (asserts) an invariant is called an assertion (or just an assert)." --- >p163 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p163.cpp && ./a.out error: bad arguments for mcf --- >p163 example code https://rentry.org/PPP2_p163 https://coliru.stacked-crooked.com/a/8b2b7982a1ecc138
Open file (146.44 KB 1140x1387 PPP2_p164.png)
>>20713 >"The question is just what to do if the pre-condition is violated (doesn’t hold). We basically have two choices: > 1. Ignore it (hope/assume that all callers give correct arguments). > 2. Check it (and report the error somehow). >Looking at it this way, argument types are just a way of having the compiler check the simplest pre-conditions for us and report them at compile time." --- >p164 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p164.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:28:41: error: invalid conversion from 'const char*' to 'int' [-fpermissive] 28 | int x = my_complicated_function(1, 2, "horsefeathers"); | ^~~~~~~~~~~~~~~ | | | const char* main.cpp:17:47: note: initializing argument 3 of 'int my_complicated_function(int, int, int)' 17 | int my_complicated_function(int a, int b, int c) | ~~~~^ --- >p164 example code https://rentry.org/PPP2_p164 https://coliru.stacked-crooked.com/a/efd0502282551205
Open file (160.57 KB 1140x1587 PPP2_p165a.png)
>>20715 >"Writing pre-conditions (even as comments) also has a significant benefit for the quality of your programs: it forces you to think about what a function requires." >"If you can’t state that simply and precisely in a couple of comment lines, you probably haven’t thought hard enough about what you are doing." >"We did mention that we hated debugging; explicitly stating pre-conditions helps in avoiding design errors as well as catching usage errors early." >"Writing [the more complex function] saves you time and grief compared with the apparently simpler [function]" --- >p165a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p165a.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:36:41: error: invalid conversion from 'const char*' to 'int' [-fpermissive] 36 | int x = my_complicated_function(1, 2, "horsefeathers"); | ^~~~~~~~~~~~~~~ | | | const char* main.cpp:29:47: note: initializing argument 3 of 'int my_complicated_function(int, int, int)' 29 | int my_complicated_function(int a, int b, int c) { | ~~~~^ --- >p165a example code https://rentry.org/PPP2_p165a https://coliru.stacked-crooked.com/a/9ef43fc55973c536
Open file (236.47 KB 1140x2187 PPP2_p165b.png)
>"[The commented-out function here] checks its pre-condition, but it doesn’t state it in the comment (that may be OK for such a short function) and it assumes that the computation is correct (that’s probably OK for such a trivial computation)." >"However, we could be a bit more explicit" >"Pre- and post-conditions provide basic sanity checks in code. As such they are closely connected to the notion of invariants (§9.4.3), correctness (§4.2, §5.2), and testing (Chapter 26)." Stop. Try and understand this example fully, Anon. If you can, then you're well on your way to understanding errors and how to detect them! :^) --- >p165b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p165b.cpp && ./a.out error: area() pre-condition --- >p165b example code https://rentry.org/PPP2_p165b https://coliru.stacked-crooked.com/a/4b423050b038f172
Edited last time by Chobitsu on 02/25/2023 (Sat) 13:43:09.
Open file (73.94 KB 1140x713 PPP2_foo_main.png)
Open file (24.25 KB 1140x438 PPP2_foo_h.png)
Open file (31.86 KB 1140x513 PPP2_foo_cpp.png)
Example code to demonstrate multifile builds using Coliru. >see the mini FAQ explanation ITT if needed (>>18749) note: Remember to clear the command line of each extra file before sharing it, Anon. :^) --- >command line + possible output: ln -s /Archive2/97/69e1b3dc7af21f/main.cpp Foo.h ln -s /Archive2/33/abc01b07a06392/main.cpp Foo.cpp g++ -std=c++20 -O2 -Wall -pedantic Foo.cpp main.cpp && ./a.out ctor'd a Foo --- >example code https://rentry.org/PPP2_foo_main https://rentry.org/PPP2_foo_h https://rentry.org/PPP2_foo_cpp https://coliru.stacked-crooked.com/a/1f5491740d5e0dcd https://coliru.stacked-crooked.com/a/9769e1b3dc7af21f https://coliru.stacked-crooked.com/a/33abc01b07a06392
Open file (107.65 KB 1140x1466 PPP2_calculator_cli.png)
This is a rendition of Bjarne's basic mathematical calculator project into a maintainable format. The project spans two full chapters in the book, and is presented in a very methodical, bottom-up approach to reasoning about how to solve everything step-by-step. The primary focus of the project is to teach you how to parse formal Language Grammars using your new programming skills. It's presented ITT here in advance before those chapters, so that you can 'look ahead' and get an idea where it's all going. If you're ever scratching your head trying to figure out one of the steps (to come), you can always look back here to see how it was solved in the end. BTW, this is a working calculator. Try different entries for the here string arguments at the end of the command line below. >(protip: simply modify what's already given as an example) You can just follow the link below to Coliru and tinker around with things there. Caution As a novice, it's not intended that you understand how all this works yet (though you will before long). For now, its simply posted here as a reference to assist with understanding the answers to some questions that will pop up for you as you work through these 2 chapters of the book. So glance at it if you care to, otherwise just refer back here afterwards as-needed. The textbook's material picks up a couple posts ahead at § 6.1 A problem - p175 (>>21320), so you can just skip over to there instead if you'd like. Cheers. --- >Calculator project command line + possible output: ln -s /Archive2/b4/f1e7d567fd6bd2/main.cpp Calculator.h ln -s /Archive2/68/52ff216ce3687f/main.cpp calc_util.h ln -s /Archive2/56/2e219ca8d64e0b/main.cpp Calculator.cpp ln -s /Archive2/68/b228c371f01ef5/main.cpp calc_util.cpp g++ -std=c++20 -O2 -Wall -pedantic calc_util.cpp Calculator.cpp main.cpp && ./a.out <<< '1+2; 5%8; 2*(3/4-5); q' > = 3 > = 5 > = -8.5 > --- >Calculator project example code https://rentry.org/PPP2_calculator_cli https://coliru.stacked-crooked.com/a/213a22dd10a6f316
Edited last time by Chobitsu on 03/17/2023 (Fri) 20:03:23.
Open file (427.67 KB 1140x3841 PPP2_Calculator_h.png)
Open file (382.10 KB 1140x2941 PPP2_calc_util_h.png)
Edited last time by Chobitsu on 03/12/2023 (Sun) 21:48:08.
Open file (774.33 KB 1140x7491 PPP2_Calculator_cpp.png)
Open file (208.00 KB 1140x2016 PPP2_calc_util_cpp.png)
Edited last time by Chobitsu on 03/12/2023 (Sun) 21:48:32.
Open file (64.47 KB 1140x687 PPP2_p175.png)
>"Writing a program starts with a problem; that is, you have a problem that you’d like a program to help solve." >"Understanding that problem is key to a good program." >"At this stage [of the book], what would be a good program to look at?" >"We chose “Get the computer to do ordinary arithmetic on expressions we type in”; that is, we want to write a simple calculator." --- >p175 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p175.cpp && ./a.out 14.4 --- >p175 example code https://rentry.org/PPP2_p175 https://coliru.stacked-crooked.com/a/32d99c70644a8855
Edited last time by Chobitsu on 03/13/2023 (Mon) 03:47:03.
Open file (68.89 KB 1140x737 PPP2_p178.png)
>"How do we want to interact with the calculator?" >"That’s easy: we know how to use cin and cout, but graphical user interfaces (GUIs) are not explained until Chapter 16, so we’ll stick to the keyboard and a console window." >"Given expressions as input from the keyboard, we evaluate them and write out the resulting value to the screen." --- >p178 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p178.cpp && ./a.out 4 8 0 --- >p178 example code https://rentry.org/PPP2_p178 https://coliru.stacked-crooked.com/a/19ba9c3f6e04adf4
Open file (102.01 KB 1140x1062 PPP2_p179.png)
>"At this point, we are not really ready to write the calculator program." >"We simply haven’t thought hard enough, but thinking is hard work and – like most programmers – we are anxious to write some code." >"So let’s take a chance, write a simple calculator, and see where it leads us." >"This (sort of) works! So what if this program isn’t quite complete? It feels great to get something running! Maybe this programming and computer science stuff is easier than the rumors say." >"Well, maybe, but let’s not get too carried away by an early success." :^) --- >p179 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p179.cpp && ./a.out Please enter expression (we can handle + and –): Result: 0 --- >p179 example code https://rentry.org/PPP2_p179 https://coliru.stacked-crooked.com/a/fcea610bf65a7100
Edited last time by Chobitsu on 03/17/2023 (Fri) 11:33:14.
Open file (182.47 KB 1140x1762 PPP2_p180.png)
>>21325 >"In particular, we know that we should always check that our input is reasonable (in our hurry, we “forgot”) and that testing a value against many constants is best done by a switch-statement rather than an if-statement." >"The “chaining” of operations, such as 1+2+3+4, we will handle by adding the values as they are read; that is, we start with 1, see +2 and add 2 to 1 (getting an intermediate result 3), see +3 and add that 3 to our intermediate result (a 6), and so on." --- Stop If you've been doing really well and have understood everything that's going on so far -- or at least most of it heh -- then proceed through with the next post and so on. OTOH, if you're struggling a bit with things, then I suggest you skip over the next few posts and proceed directly to § 6.5.2.1 Expressions: first try (>>21444). This is where the author begins to methodically work out the solutions for the calculator, moving through things one step at a time. It's a more relaxed pace of things tbh. :^) But please at least read through the book from this point (p 180) to p 197 (where we pick back up)! It's OK if you can't understand it all yet through that section, but it's important to at least get your brain used to seeing the ideas. The reason for all this is that in my effort to make most of the programs over the next few posts actually work, I needed to throw in some already-implemented functions so everything would build & run. This is a lot of new information & complexity that's just kind of dropped right into the novice's lap. I apologize for that necessity. Again, if you're struggling then just skip ahead to the provided crosslink, where we'll all pick back up again. See you there Anon! Cheers. :^) --- >p180 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p180.cpp && ./a.out Please enter expression (we can handle +, –, *, and /) add an x to end expression (e.g., 1+2*3x): terminate called after throwing an instance of 'std::runtime_error' what(): no first operand bash: line 7: 5232 Aborted (core dumped) ./a.out --- >p180 example code https://rentry.org/PPP2_p180 https://coliru.stacked-crooked.com/a/d6111bb450d89ffc
Edited last time by Chobitsu on 03/17/2023 (Fri) 19:25:37.
Open file (158.72 KB 1140x1487 PPP2_p183.png)
>"What should a token look like in our program? In other words, what would we like our Token type to be?" >"A Token must be able to represent operators, such as + and –, and numeric values, such as 42 and 3.14." >"The obvious implementation is something that can represent what “kind” a token is and hold the numeric value for tokens that have one" --- >p183 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p183.cpp && ./a.out 3.14 --- >p183 example code https://rentry.org/PPP2_p183 https://coliru.stacked-crooked.com/a/2aee019423e3b12d
Edited last time by Chobitsu on 03/14/2023 (Tue) 01:50:08.
Open file (109.82 KB 1140x1137 PPP2_p184.png)
>>21327 >"Note that for simple tokens, such as +, we don’t need the value, so we don’t use its value member." >"We needed a character to mean “number” and picked '8' just because '8' obviously isn’t an operator or a punctuation character. Using '8' to mean “number” is a bit cryptic, but it’ll do for now." >"Token is an example of a C++ user-defined type. A user-defined type can have member functions (operations) as well as data members. There can be many reasons for defining member functions." >"Here, we’ll just provide two member functions to give us a more convenient way of initializing Tokens" --- >p184 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p184.cpp && ./a.out + 0 8 11.5 --- >p184 example code https://rentry.org/PPP2_p184 https://coliru.stacked-crooked.com/a/bd0de0fd063c4d5d
Open file (226.23 KB 1140x1912 PPP2_p185.png)
>"How would we use Tokens in the calculator?" >"We can read input into a vector of Tokens" >"we could find the multiply operation by a simple loop" >"Yes, but now what? What do we do with that product d?" --- >p185 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p185.cpp && ./a.out /tmp/ccNN7d2e.o: In function `main': main.cpp:(.text.startup+0x51): undefined reference to `get_token()' collect2: error: ld returned 1 exit status --- >p185 example code https://rentry.org/PPP2_p185 https://coliru.stacked-crooked.com/a/7a76796de26cd2e9
Open file (273.12 KB 1140x2587 PPP2_p188.png)
>"There is a standard answer to the question of how to make sense of expressions:" >"first input characters are read and assembled into tokens (as [] discovered)." >"A token is a sequence of characters that represents something we consider a unit, such as a number or an operator." --- >p188 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p188.cpp && ./a.out <<< '45+11.5/7;' Please enter expression (we can handle +, –, *, and /), then add a ; to print (e.g., 1+2*3;): the Tokens are listed as: t.kind : t.value 8 : 45 + : 0 8 : 11.5 / : 0 8 : 7 --- >p188 example code https://rentry.org/PPP2_p188 https://coliru.stacked-crooked.com/a/dcc8175e6af67d5d
Open file (298.16 KB 1140x2712 PPP2_p189.png)
>>21366 >"How do you read a grammar? Basically, given some input, you start with the “top rule,” Expression, and search through the rules to find a match for the tokens as they are read." >"Reading a stream of tokens according to a grammar is called parsing, and a program that does that is often called a parser or a syntax analyzer." >"Our parser reads the tokens from left to right, just like we type them and read them." >"Let’s try something really simple: Is 2 an expression?" >"yes, according to our grammar, 2 is an expression. We can illustrate the progression through the grammar [with the parsing tree given.]"' --- >p189 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p189.cpp && ./a.out 2 --- >p189 example code https://rentry.org/PPP2_p189 https://coliru.stacked-crooked.com/a/01d2f651e3a2560c
Edited last time by Chobitsu on 03/15/2023 (Wed) 00:24:25.
Open file (383.63 KB 1140x3562 PPP2_p191.png)
>>21375 >"Let’s try something a bit more complicated: Is 2+3 an Expression? Naturally, much of the reasoning is the same as for 2" >"Again, we can illustrate this reasoning graphically (leaving out the floating-point literal to Number rule to simplify)" >"[So yes,] an Expression followed by + followed by a Term is an Expression." >"The real reason we are interested in grammars is that they can solve our problem of how to correctly parse expressions"' --- >p191 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p191.cpp && ./a.out <<< '2+3;' enter: 2+3; 5 --- >p191 example code https://rentry.org/PPP2_p191 https://coliru.stacked-crooked.com/a/63d9276ea3015320
Edited last time by Chobitsu on 03/17/2023 (Fri) 20:10:54.
Open file (572.32 KB 1140x4837 PPP2_p192.png)
>>21403 >"Is 45+11.5*7 an Expression?" >"In particular, it is an Expression that first does the multiplication 11.5*7 [(via the Term*Primary rule),] and then the addition 45+[80.5], just as if we had written 45+(11.5*7)." >"[So yes,] an Expression followed by + followed by a Term*Primary is an Expression." >"we were not really trying [here] to teach you to understand 2+2 or 45+11.5*7. Obviously, you knew that already."' >"We were trying to find a way for the computer to “understand” 45+11.5*7 and all the other complicated expressions you might give it to evaluate." --- >p192 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p192.cpp && ./a.out <<< '45+11.5*7;' enter: 45+11.5*7; 125.5 --- >p192 example code https://rentry.org/PPP2_p192 https://coliru.stacked-crooked.com/a/2a836369dcc3989e
Open file (590.91 KB 1140x5962 PPP2_p193.png)
>"If you have never before worked with grammars, we expect that your head is now spinning." >"In fact, it may be spinning even if you have seen a grammar before, but take a look at the [provided] grammar for a very small subset of English" >"A sentence is built from parts of speech (e.g., nouns, verbs, and conjunctions)." >"A sentence can be parsed according to these rules to determine which words are nouns, verbs, etc." --- >p193 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p193.cpp && ./a.out <<< 'birbs fly but fish swim ;' enter: birbs fly but fish swim ; (leave a space before ;) n birbs v fly c but n fish v swim sentence: birbs fly but fish swim ; --- >p193 example code https://rentry.org/PPP2_p193 https://coliru.stacked-crooked.com/a/b65dc739966c68c0
Open file (517.63 KB 1140x5237 PPP2_p195.png)
>"How did we pick those expression grammar rules?" >"“Experience” is the honest answer. The way we do it is simply the way people usually write expression grammars." >"However, writing a simple grammar is pretty straightforward" >"We simply put tokens in (double) quotes and start with the first rule. Alternatives are put on separate lines." --- >p195 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p195.cpp && ./a.out <<< '{A, A, A, A, B}' enter: {A, A, A, A, B} { A, A, A, A, B } : this is a valid list --- >p195 example code https://rentry.org/PPP2_p195 https://coliru.stacked-crooked.com/a/d47677084806904d
Edited last time by Chobitsu on 03/17/2023 (Fri) 07:41:45.
Open file (233.83 KB 1140x1712 PPP2_p197.png)
>"To implement our calculator, we need four functions: one to read tokens plus one for each rule in our grammar:" >"Let’s first write expression()." >"Since this is our first attempt to turn a set of grammar rules into code, we’ll proceed through a couple of false starts. That’s the way it usually goes with new techniques, and we learn useful things along the way." >"In particular, a novice programmer can learn a lot from looking at the dramatically different behavior of similar pieces of code. Reading code is a useful skill to cultivate." --- >p197 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p197.cpp && ./a.out main.cpp: In function 'double expression()': main.cpp:35:8: warning: infinite recursion detected [-Winfinite-recursion] 35 | double expression() { | ^~~~~~~~~~ main.cpp:36:27: note: recursive call 36 | double left = expression(); // read and evaluate an Expression | ~~~~~~~~~~^~ /tmp/ccDIgtqr.o: In function `expression()': main.cpp:(.text+0x10): undefined reference to `get_token()' main.cpp:(.text+0x31): undefined reference to `term()' main.cpp:(.text+0x51): undefined reference to `term()' collect2: error: ld returned 1 exit status --- >p197 example code https://rentry.org/PPP2_p197 https://coliru.stacked-crooked.com/a/c5a4d33f0784f7a9
Open file (234.26 KB 1140x1737 PPP2_p198.png)
>"In fact, this expression() will never get beyond its first line: expression() starts by calling expression() which starts by calling expression() and so on “forever.”" >"This is called an infinite recursion and will in fact terminate after a short while when the computer runs out of memory to hold the “never-ending” sequence of calls of expression()." >"The term recursion is used to describe what happens when a function calls itself. Not all recursions are infinite, and recursion is a very useful programming technique (see §8.5.8)." >"So what do we do? Every Term is an Expression, but not every Expression is a Term; that is, we could start looking for a Term and look for a full Expression only if we found a + or a –." --- >p198 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p198.cpp && ./a.out /tmp/cceRLSTF.o: In function `expression()': main.cpp:(.text+0x5): undefined reference to `term()' main.cpp:(.text+0x10): undefined reference to `get_token()' collect2: error: ld returned 1 exit status --- >p198 example code https://rentry.org/PPP2_p198 https://coliru.stacked-crooked.com/a/f5188f612b4533c5
Open file (223.33 KB 1140x1787 PPP2_p199.png)
>"So we got a very nice program that just didn’t do the right thing. That’s dangerous." >"It is especially dangerous because it gives the right answer in many cases. For example, 1+2+3 gives the right answer (6) because 1+(2+3) equals (1+2)+3." >"What fundamentally, from a programming point of view, did we do wrong? We should always ask ourselves this question when we have found an error. That way we might avoid making the same mistake again, and again, and again." >"Fundamentally, we just looked at the code and guessed. That’s rarely good enough!" >"So, what now?" >"Look again at the grammar (the correct grammar in §6.5.2): any Expression starts with a Term and such a Term can be followed by a + or a –. So, we have to look for a Term, see if it is followed by a + or a –, and keep doing that until there are no more plusses or minuses." --- >p199 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p199.cpp && ./a.out /tmp/ccFvCha2.o: In function `expression()': main.cpp:(.text+0x5): undefined reference to `term()' main.cpp:(.text+0x10): undefined reference to `get_token()' main.cpp:(.text+0x25): undefined reference to `term()' main.cpp:(.text+0x3a): undefined reference to `get_token()' main.cpp:(.text+0x43): undefined reference to `term()' collect2: error: ld returned 1 exit status --- >p199 example code https://rentry.org/PPP2_p199 https://coliru.stacked-crooked.com/a/6812e3612948b70b
Edited last time by Chobitsu on 03/31/2023 (Fri) 12:14:00.
Open file (226.35 KB 1140x1862 PPP2_p200.png)
>>21636 >"This is a bit messier: we had to introduce a loop to keep looking for plusses and minuses. We also got a bit repetitive: we test for + and – twice and twice call get_token()." >"Because it obscures the logic of the code, let’s just get rid of the duplication of the test for + and –" >"What we have done is to remove the mention of expression() within expression() and replace it with a loop. In other words, we translated the Expression in the grammar rules for Expression into a loop looking for a Term followed by a + or a –." This last bit mentioned is a very key technique used here, for how to approach this entire concept generally. Please be sure you understand it before proceeding Anon. :^) --- >p200 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p200.cpp && ./a.out /tmp/ccQYnbix.o: In function `expression()': main.cpp:(.text+0x5): undefined reference to `term()' main.cpp:(.text+0x10): undefined reference to `get_token()' main.cpp:(.text+0x31): undefined reference to `term()' main.cpp:(.text+0x49): undefined reference to `term()' collect2: error: ld returned 1 exit status --- >p200 example code https://rentry.org/PPP2_p200 https://coliru.stacked-crooked.com/a/3fe7fdbfd496ad2d
Edited last time by Chobitsu on 04/09/2023 (Sun) 23:23:15.
Open file (266.64 KB 1140x2462 PPP2_p201.png)
>"The grammar rule for Term is very similar to the Expression rule consequently, the code should be very similar also." >"Unfortunately, this doesn’t compile: the remainder operation (%) is not defined for floating-point numbers." >"we could simply leave % out of our calculator. Let’s take the simplest choice for now." >"However, an experienced programmer will notice an undesirable detail that makes [this version of] term() unacceptable. What would happen if you entered 2/0?" --- >p201 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p201.cpp && ./a.out /tmp/ccdJLsdO.o: In function `term()': main.cpp:(.text+0x5): undefined reference to `primary()' main.cpp:(.text+0x10): undefined reference to `get_token()' main.cpp:(.text+0x31): undefined reference to `primary()' main.cpp:(.text+0x49): undefined reference to `primary()' /tmp/ccdJLsdO.o: In function `expression()': main.cpp:(.text+0x70): undefined reference to `get_token()' collect2: error: ld returned 1 exit status --- >p201 example code https://rentry.org/PPP2_p201 https://coliru.stacked-crooked.com/a/d5a29d12c851f072
Open file (293.61 KB 1140x2962 PPP2_p202.png)
>>21662 >"You can’t divide by zero." >"If you try, the computer hardware will detect it and terminate your program with a somewhat unhelpful error message." >"So, we’d better check and give a decent error message" >"Why did we put the statements handling / into a block? The compiler insists." >"If you want to define and initialize variables within a switch-statement, you must place them inside a block." --- >p202 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p202.cpp && ./a.out /tmp/cc4ZfTd7.o: In function `term()': main.cpp:(.text+0x7): undefined reference to `primary()' main.cpp:(.text+0x12): undefined reference to `get_token()' main.cpp:(.text+0x31): undefined reference to `primary()' main.cpp:(.text+0x49): undefined reference to `primary()' /tmp/cc4ZfTd7.o: In function `expression()': main.cpp:(.text+0x80): undefined reference to `get_token()' collect2: error: ld returned 1 exit status --- >p202 example code https://rentry.org/PPP2_p202 https://coliru.stacked-crooked.com/a/c6d3a7d9cc585a10
Open file (348.21 KB 1140x3537 PPP2_p203a.png)
>"The grammar rule for primary expressions is also simple" >"The code that implements it is a bit messy because there are more opportunities for syntax errors" >"Basically there is nothing new compared to expression() and term(). We use the same language primitives, the same way of dealing with Tokens, and the same programming techniques." --- >p203a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p203a.cpp && ./a.out /tmp/ccFR2TxI.o: In function `term()': main.cpp:(.text+0x11): undefined reference to `get_token()' /tmp/ccFR2TxI.o: In function `expression()': main.cpp:(.text+0xb0): undefined reference to `get_token()' /tmp/ccFR2TxI.o: In function `primary()': main.cpp:(.text+0x106): undefined reference to `get_token()' main.cpp:(.text+0x12c): undefined reference to `get_token()' collect2: error: ld returned 1 exit status --- >p203a example code https://rentry.org/PPP2_p203a https://coliru.stacked-crooked.com/a/a51235c6a0466ffb
Open file (351.27 KB 1140x3812 PPP2_p203b.png)
>"To run these calculator functions, we need to implement get_token() and provide a main()." >"The main() is trivial: we just keep calling expression() and printing out its result:" >"Unsurprisingly, this first version of the calculator doesn’t work quite as we expected." >"So we shrug and ask, “Why not?” or rather, “So, why does it work the way it does?” and “What does it do?”" --- >p203b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p203b.cpp && ./a.out Bad token --- >p203b example code https://rentry.org/PPP2_p203b https://coliru.stacked-crooked.com/a/7ed96546edbce5d9
Open file (487.61 KB 1140x5087 PPP2_p206.png)
>>21666 >"When the Token returned by get_token() is not a + or a – we just return. We don’t use that token and we don’t store it anywhere for any other function to use later." >"That’s not smart. Throwing away input without even determining what it is can’t be a good idea." >"A quick look shows that term() has exactly the same problem. That explains why our calculator ate two tokens for each that it used. Let us modify expression() so that it doesn’t “eat” tokens. In addition, we must make the same change to term()" >"Where would we put that next token (t) when the program doesn’t need it?" >"What we want is an input stream that deals with tokens and that you can put an already read token back into. [This is our new user-defined type 'Token_stream'.]" --- >p206 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p206.cpp && ./a.out Bad token --- >p206 example code https://rentry.org/PPP2_p206 https://coliru.stacked-crooked.com/a/38205c2eab3a413d
Open file (510.94 KB 1140x5412 PPP2_p209.png)
>"We still have a token-look-ahead problem. However, this time the problem is not that our code “eats” characters, but that it doesn’t get any output for an expression until we enter the following expression." >"The result of an expression isn’t printed immediately; the output is postponed until the program has seen the first token of the next expression. Unfortunately, the program doesn’t see that token until we hit Return after the next expression." >"How can we fix this? One obvious solution is to require a “print command.” [;]" >"And while we are at it, let’s add an “exit command” to allow for graceful exit. [q]" --- >p209 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p209.cpp && ./a.out Bad token --- >p209 example code https://rentry.org/PPP2_p209 https://coliru.stacked-crooked.com/a/718d593d8109280a
Open file (227.73 KB 1140x1737 PPP2_p210.png)
>"Before further improving our calculator, let us show the implementation of Token_stream. After all, nothing – nothing at all – works until we get correct input." >"What we need is something that reads characters from the standard input, cin, and presents the program with the next token when it asks for it." >"In addition, we saw that we – that is, our calculator program – often read a token too many, so that we must be able to put it back for later use." >"A C++ user-defined type often consists of two parts: the public interface (labeled public:) and the implementation details (labeled private:)." >"The idea is to separate what a user of a type needs for convenient use from the details that we need in order to implement the type, but that we’d rather not have users mess with" --- >p210 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p210.cpp && ./a.out /tmp/ccY9zXib.o: In function `main': main.cpp:(.text.startup+0xa): undefined reference to `Token_stream::Token_stream()' main.cpp:(.text.startup+0x14): undefined reference to `Token_stream::get()' main.cpp:(.text.startup+0x20): undefined reference to `Token_stream::putback(Token)' collect2: error: ld returned 1 exit status --- >p210 example code https://rentry.org/PPP2_p210 https://coliru.stacked-crooked.com/a/0baf473ece0f52b9
Open file (216.51 KB 1140x2137 PPP2_p211.png)
>"Now, we need to implement those three Token_stream functions." >"How do we represent a Token_stream? That is, what data do we need to store in a Token_stream for it to do its job?" >"we just need space for one Token and an indicator of whether that space is full or empty" --- >p211 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p211.cpp && ./a.out --- >p211 example code https://rentry.org/PPP2_p211 https://coliru.stacked-crooked.com/a/8498de946abde6ba
Open file (292.09 KB 1140x2887 PPP2_p212.png)
>"All the real work is done by get()." >"If there isn’t already a Token in Token_stream::buffer, get() must read characters from cin and compose them into Tokens" >" " --- >p212 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p212.cpp && ./a.out --- >p212 example code https://rentry.org/PPP2_p212 https://coliru.stacked-crooked.com/a/344d4baf5da95971
Open file (309.73 KB 1140x3112 PPP2_p214.png)
>"Now we just have to deal with numbers. That’s actually not that easy." >"We could spend hours or days to get this right, but fortunately, we don’t have to. Input streams know what C++ literals look like and how to turn them into values of type double." >"All we have to do is to figure out how to tell cin to do that for us inside get()" >"instead of doing any messy arithmetic, we just put the initial [numeric] character back into cin, and then let cin read the [entire] number [in all at once]." --- >p214 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p214.cpp && ./a.out --- >p214 example code https://rentry.org/PPP2_p214 https://coliru.stacked-crooked.com/a/290b4884b4a254e5
Open file (474.66 KB 1140x5362 PPP2_p215.png)
>"Sometimes, the proverb says, it’s hard to see the forest for the trees. Similarly, it is easy to lose sight of a program when looking at all its functions, classes, etc." >"So, let’s have a look at the program with its details omitted" note: within Coliru, you can collapse functions to look similar to the book's example given. >"The order of the declarations is important. You cannot use a name before it has been declared, so ts must be declared before ts.get() uses it, and error() must be declared before the parser functions because they all use it." >"There is an interesting loop in the call graph: expression() calls term() which calls primary() which calls expression()." >"We chose to declare (“forward declare”) expression()." Stay strong. That's it for chapter 6, and we're now more than halfway complete with the project. I know that was a lot to take in Anon, but we'll wrap everything up for our calculator project in the next chapter. BTW, you do want your waifu to be able to do arithmetic for you Anon, right? :^) Stay tuned! --- >p215 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p215.cpp && ./a.out <<< '2+2;q' = 4 primary expected --- >p215 example code https://rentry.org/PPP2_p215 https://coliru.stacked-crooked.com/a/f8eec99cc12c5093
Edited last time by Chobitsu on 04/01/2023 (Sat) 22:05:11.
Open file (460.42 KB 1140x5412 PPP2_p223.png)
>"When your program first starts running “reasonably,” you’re probably about halfway finished. For a large program or a program that could do harm if it misbehaved, you will be nowhere near halfway finished." >"Once the program “basically works,” the real fun begins! That’s when we have enough working code to experiment with ideas." >"In this chapter, we will guide you through the considerations a professional programmer might have trying to improve the calculator from Chapter 6." >"We would [] like a short “prompt” to indicate that the program wants input. The > character is often used as a prompt" --- >p223 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p223.cpp && ./a.out <<< '2+2;q' > > = 4 primary expected --- >p223 example code https://rentry.org/PPP2_p223 https://coliru.stacked-crooked.com/a/b936e2970a2a7baa
Open file (427.67 KB 1140x5312 PPP2_p228.png)
>"We added those “print” and “quit” commands rather quickly to get the calculator to work (§6.7). Now we are paying for that haste." >"If we find a semicolon, we straightaway proceed to call expression() without checking for q." >"... So, we should test for q after testing for a semicolon." >"While we were at it, we felt the need to simplify the logic a bit" --- >p228 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p228.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p228 example code https://rentry.org/PPP2_p228 https://coliru.stacked-crooked.com/a/cfe6406e373bd3e1
Edited last time by Chobitsu on 04/02/2023 (Sun) 07:03:44.
Open file (438.73 KB 1140x5413 PPP2_p230.png)
>"If you tested the calculator, you found that it couldn’t handle negative numbers elegantly. That’s not acceptable." >"In this case, we basically need to modify the grammar to allow unary minus [and unary plus]. The simplest change seems to be in Primary." >"That’s so simple that it actually worked the first time." --- >p230 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p230.cpp && ./a.out <<< '-1/2;q' > = -0.5 >  --- >p230 example code https://rentry.org/PPP2_p230 https://coliru.stacked-crooked.com/a/116af9c2f367604b
Edited last time by Chobitsu on 04/02/2023 (Sun) 07:03:26.
Open file (495.00 KB 1140x5912 PPP2_p231.png)
>"When we first analyzed the ideals for a calculator, we wanted the remainder (modulo) operator: %. However, % is not defined for floating-point numbers, so we backed off." >"Now we can consider it again." >"But how should we handle operands that are not integers?" >"we can prohibit the use of % on a floating-point argument." --- >p231 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p231.cpp && ./a.out <<< '2%3;q' > = 2 >  --- >p231 example code https://rentry.org/PPP2_p231 https://coliru.stacked-crooked.com/a/f3171a0c49c4ec3f
Open file (541.97 KB 1140x6112 PPP2_p232.png)
>"Looking back, we find the use of '8' to indicate a Token containing a numeric value odd. It doesn’t really matter what value is used to indicate a number Token as long as the value is distinct" >"What we should have done was to introduce a symbolic name for the constant we used to represent a number" >"Introducing symbolic names [] makes the code easier to read [(and therefore less error-prone)]." --- >p232 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p232.cpp && ./a.out <<< '2+2;q' > = 4 > = primary expected --- >p232 example code https://rentry.org/PPP2_p232 https://coliru.stacked-crooked.com/a/b1b666352d559843
Open file (559.19 KB 1140x6312 PPP2_p234.png)
>"The functions we use should reflect the structure of our program, and the names of the functions should identify the logically separate parts of our code. Basically, our program so far is rather good in this respect [except main()]" >"Ideally, a function performs a single logical action" >"The obvious solution is to make the calculation loop into a separate function calculate()" >"This reflects the structure much more directly and is therefore easier to understand." --- >p234 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p234.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p234 example code https://rentry.org/PPP2_p234 https://coliru.stacked-crooked.com/a/accae5156e3f1a11
Open file (554.41 KB 1140x6437 PPP2_p235.png)
>"Code that is hard to read is where bugs can more easily hide. Using one line per case and adding a couple of comments help." >"We could of course have put each digit case on a separate line also, but that didn’t seem to buy us any clarity." >"When we clean up code, we might accidentally introduce errors. Always retest the program after cleanup. Remember: Test early and often." --- >p235 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p235.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p235 example code https://rentry.org/PPP2_p235 https://coliru.stacked-crooked.com/a/36fb42a733fbc064
Open file (669.23 KB 1140x7637 PPP2_p237.png)
>"Good comments are an important part of writing code." >"When you go back to the code to clean it up is an excellent time to look at each part of the program to see if the comments you originally wrote are [still valid]" >"Comments are for things that code expresses poorly. An example is intent: code says what it does, not what it was intended to do (§5.9.1)." >"[Remember] that the comments are not the code. [The code already says exactly what it does; comments are for the intent behind that code.]" --- >p237 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p237.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p237 example code https://rentry.org/PPP2_p237 https://coliru.stacked-crooked.com/a/4cec91091f0944de
Open file (701.83 KB 1140x8012 PPP2_p239.png)
>"Why do we exit when we find an error? That seemed simple and obvious at the time, but why? Couldn’t we just write an error message and carry on? After all, we often make little typing errors and such an error doesn’t mean that we have decided not to do a calculation." >"So let’s try to recover from an error." >"That basically means that we have to catch exceptions and continue after we have cleaned up any messes that were left behind." >"If we want to recover from errors, calculate() must catch exceptions and try to clean up the mess before trying to evaluate the next expression" --- >p239 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p239.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p239 example code https://rentry.org/PPP2_p239 https://coliru.stacked-crooked.com/a/a9e5ec6134c06003
Open file (720.40 KB 1140x8362 PPP2_p240.png)
>>21693 >"Unfortunately, that [didn't] work all that well." >"We could try more elaborate trys and catches, but basically we are heading into an even bigger mess. Errors are hard to handle, and errors during error handling are even worse than other errors." >"So, let’s try to devise some way to flush characters out of a Token_stream that couldn’t possibly throw an exception." >"Dealing with errors is always tricky. It requires much experimentation and testing because it is extremely hard to imagine what errors can occur. Trying to make a program foolproof is always a very technical activity; amateurs typically don’t care. Quality error handling is one mark of a professional." --- >p240 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p240.cpp && ./a.out <<< '2+2;q' > = 4 >  --- >p240 example code https://rentry.org/PPP2_p240 https://coliru.stacked-crooked.com/a/a6c816996d2c565c
Open file (1.01 MB 1140x11337 PPP2_p242.png)
>"We now have a program that works quite well; how can we improve it? The first wish list for the calculator included variables." >"Having variables gives us better ways of expressing longer calculations. Similarly, for scientific calculations, we’d like built-in named values, such as pi and e, just as we have on scientific calculators." >"Adding variables and constants is a major extension to the calculator. It will touch most parts of the code. This is the kind of extension that we should not embark on without good reason and sufficient time." >"Obviously, the key to both variables and built-in constants is for the calculator program to keep (name,value) pairs so that we can access the value given the name." >"General rules tend to keep code simple, whereas special cases tend to lead to complications." >"This mechanism for keeping track of Variables is what is often called a symbol table and could be radically simplified by the use of a standard library map; see §21.6.1." --- >p242 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out <<< 'let foo = 242;q' > = 242 >  --- >p242 example code https://rentry.org/PPP2_p242 https://coliru.stacked-crooked.com/a/ec17961d568e4f3b
Open file (1.02 MB 1140x11437 PPP2_p247.png)
>"This is all very good, but unfortunately, it doesn’t quite work. By now, that shouldn’t come as a surprise. Our first cut never — well, hardly ever — works." >"What characters can be part of a name? Letters? Certainly. Digits? Certainly, as long as they are not the starting character." >"Instead of reading directly into the string s, we read characters and put those into s as long as they are letters or digits." --- >p247 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p247.cpp && ./a.out <<< 'let foo = 42; foo * 2;q' > = 42 > = 84 >  --- >p247 example code https://rentry.org/PPP2_p247 https://coliru.stacked-crooked.com/a/da5bdfe2e6bb3b8f
Open file (1.03 MB 1140x11537 PPP2_p250.png)
>"Now that we have names, we can easily predefine a few common ones. For example, if we imagine that our calculator will be used for scientific calculations, we’d want pi and e." >"Where in the code would we define those? In main() before the call of calculate() or in calculate() before the loop." >"We’ll put them in main() because those definitions really aren’t part of any calculation" Stop. You've made it to the end of the calculator project. This gives you a reasonably good grounding in the basics of how to define & parse a formal Language Grammar. This elegant programming skill will be valuable for many things in the future for you; for example, using various related-approaches in teaching your robowaifu how to understand human languages (and specifically your language ofc). Congrats Anon, you've got this by now! Now keep moving forward. :^) --- >p250 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_07/main_p250.cpp && ./a.out <<< 'let r=2;pi*(r*r);q' > = 2 > = 12.5664 >  --- >p250 example code https://rentry.org/PPP2_p250 https://coliru.stacked-crooked.com/a/8dc95f57a1c2f609
Edited last time by Chobitsu on 04/02/2023 (Sun) 15:12:19.
Open file (100.14 KB 1140x887 PPP2_p257.png)
>"Given a choice, we’d much rather talk about programming than about programming language features; that is, we consider how to express ideas as code far more interesting than the technical details of the programming language that we use to express those ideas." >"However, we don’t always have a choice. When you start programming, your programming language is a foreign language for which you need to look at “grammar and vocabulary.” This is what we will do in this chapter and the next" >"A declaration is a statement that introduces a name into a scope (§8.4)" >"- Specifying a type for what is named (e.g., a variable or a function)" >"- Optionally, specifying an initializer (e.g., an initializer value or a function body)" >tl;dr >"Before a name can be used in a C++ program, it must be declared." --- >p257 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p257.cpp && ./a.out main.cpp:14:1: error: 'vector' does not name a type 14 | vector<Token> v; // a vector-of-Tokens variable | ^~~~~~ main.cpp: In function 'int main()': main.cpp:18:3: error: 'cout' was not declared in this scope 18 | cout << f(i) << '\n'; | ^~~~ main.cpp:18:13: error: 'i' was not declared in this scope 18 | cout << f(i) << '\n'; | ^ main.cpp:18:11: error: 'f' was not declared in this scope 18 | cout << f(i) << '\n'; | ^ --- >p257 example code https://rentry.org/PPP2_p257 https://coliru.stacked-crooked.com/a/7624f4590c2316c5
Edited last time by Chobitsu on 04/04/2023 (Tue) 15:34:01.
Open file (200.41 KB 1140x2137 PPP2_p258.png)
>>21719 >"We can get cout declared by including the header std_lib_facilities.h, which contains its declaration" >"As you write real-word programs, you’ll find that most declarations are found in headers. That’s where we define interfaces to useful facilities defined “elsewhere.”" >"Why does a name have to be declared before it is used? Couldn’t we just require the language implementation to read the program (just as we do) and find the definition to see how a function must be called?" >"The C++ definition requires declaration before use" >"After all, this is already the convention for ordinary (non-program) writing: when you read a textbook, you expect the author to define terminology before using it; otherwise, you have to guess or go to the index all the time." >"The “declaration before use” rule simplifies reading for both humans and compilers." --- >p258 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p258.cpp && ./a.out main.cpp: In function 'double term()': main.cpp:59:8: warning: infinite recursion detected [-Winfinite-recursion] 59 | double term() { // definition | ^~~~ In function 'double expression()', inlined from 'double primary()' at main.cpp:56:20, inlined from 'double term()' at main.cpp:62:17: main.cpp:68:14: note: recursive call 68 | return term(); | ~~~~^~ 7 --- >p258 example code https://rentry.org/PPP2_p258 https://coliru.stacked-crooked.com/a/b2a3e7bd46aec401
Edited last time by Chobitsu on 04/04/2023 (Tue) 15:35:41.
Open file (170.65 KB 1140x1862 PPP2_p262.png)
>"The declaration of a variable or a constant specifies a name, a type, and optionally an initializer." >"Constants have the same declaration syntax as variables. They differ in having const as part of their type and requiring an initializer" >"The reason for requiring an initializer for a const is obvious: how could a const be a constant if it didn’t have a value?" --- >p262 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p262.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:19:13: error: uninitialized 'const y' [-fpermissive] 19 | const int y; // error: no initializer | ^ main.cpp:11:15: warning: unused variable 'a' [-Wunused-variable] 11 | int a; // no initializer | ^ main.cpp:12:15: warning: unused variable 'd' [-Wunused-variable] 12 | double d = 7; // initializer using the = syntax | ^ main.cpp:16:13: warning: unused variable 'x' [-Wunused-variable] 16 | const int x = 7; // initializer using the = syntax | ^ main.cpp:17:13: warning: unused variable 'x2' [-Wunused-variable] 17 | const int x2{9}; // initializer using the {} syntax | ^~ main.cpp:19:13: warning: unused variable 'y' [-Wunused-variable] 19 | const int y; // error: no initializer | ^ --- >p262 example code https://rentry.org/PPP2_p262 https://coliru.stacked-crooked.com/a/421f76e386480d5c
Open file (75.91 KB 1140x837 PPP2_p263.png)
>"You might have noticed that we often don’t provide an initializer for strings, vectors, etc. This is not an exception to the rule that variables must be initialized before use." >"What is going on here is that string and vector are defined so that variables of those types are initialized with a default value whenever we don’t supply one explicitly." >"The mechanism for guaranteeing default initialization is called a default constructor; see §9.7.3." --- >p263 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p263.cpp && ./a.out <<< 'foo bar baz' --- >p263 example code https://rentry.org/PPP2_p263 https://coliru.stacked-crooked.com/a/8538f157a073f9da
Edited last time by Chobitsu on 04/04/2023 (Tue) 17:59:41.
Open file (84.94 KB 1140x1162 PPP2_p265a.png)
>"How do we manage our declarations and definitions? After all, they have to be consistent, and in real-world programs there can be tens of thousands of declarations; programs with hundreds of thousands of declarations are not rare." >"Typically, when we write a program, most of the definitions we use are not written by us. For example, the implementations of cout and sqrt() were written by someone else many years ago. We just use them." >"The key to managing declarations of facilities defined “elsewhere” in C++ is the header." >"Since #includes logically happen before anything else a compiler does, handling #includes is part of what is called preprocessing (§A.17)." --- >p265a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p265a.cpp && ./a.out --- >p265a example code https://rentry.org/PPP2_p265a https://coliru.stacked-crooked.com/a/edc5e4d1fbe477d1
Open file (127.99 KB 1140x1462 PPP2_p265b.png)
>>21723 >"To ease consistency checking, we #include a header both in source files that use its declarations and in source files that provide definitions for those declarations. That way, the compiler catches errors as soon as possible." >"A header [file] will typically be included in many source files." >"That means that a header [file] should only contain declarations that can be duplicated in several files (such as function declarations, class definitions, and definitions of numeric constants)." --- >p265b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p265b.cpp && ./a.out main.cpp: In member function 'Token Token_stream::putback(Token)': main.cpp:25:10: error: 'class Token' has no member named 'push_back' 25 | buffer.push_back(t); // error | ^~~~~~~~~ main.cpp: In function 'int main()': main.cpp:37:16: error: 'class Token_stream' has no member named 'gett' 37 | Token t = ts.gett(); // error: no member gett | ^~~~ main.cpp:41:13: error: no matching function for call to 'Token_stream::putback()' 41 | ts.putback(); // error: argument missing | ~~~~~~~~~~^~ main.cpp:24:7: note: candidate: 'Token Token_stream::putback(Token)' 24 | Token Token_stream::putback(Token t) { | ^~~~~~~~~~~~ main.cpp:24:7: note: candidate expects 1 argument, 0 provided main.cpp:37:9: warning: unused variable 't' [-Wunused-variable] 37 | Token t = ts.gett(); // error: no member gett | ^ --- >p265b example code https://rentry.org/PPP2_p265b https://coliru.stacked-crooked.com/a/dcf6e51cfdd9ebc0
Open file (93.15 KB 1140x1112 PPP2_p266.png)
>"A scope is [simply] a region of program text. [Apart from the global scope, a scope is always demarcated by a matching-pair of braces { } .]" >"A name is [always] declared [within] a scope, and is valid (is “in scope”) from the point of its declaration until the end of the scope in which it was declared." >"The main purpose of a scope is to keep names local, so that they won’t interfere with [(clash with)] names declared elsewhere." --- >p266 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out 0 --- >p266 example code https://rentry.org/PPP2_p197 https://coliru.stacked-crooked.com/a/7e3efa22964ce666
Open file (266.62 KB 1140x3287 PPP2_p267.png)
>>21725 >"Here f()’s x is different from g()’s x. They don’t “clash” because they are not in the same scope: f()’s x is local to f and g()’s x is local to g." >"So, with the notable exception of the global scope, a scope keeps names local. For most purposes, locality is good, so keep names as local as possible." >"The larger the scope of a name is, the longer and more descriptive its name should be" --- >p267 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p267.cpp && ./a.out --- >p267 example code https://rentry.org/PPP2_p267 https://coliru.stacked-crooked.com/a/50dee6ccf87d0dca
Open file (142.50 KB 1140x2337 PPP2_p270.png)
>>21726 >"Note that most C++ constructs that define scopes nest" >"Nested blocks are unavoidable, but be suspicious of complicated nesting: it can easily hide errors." >"Note our consistent indentation to indicate nesting. Without consistent indentation, nested constructs become unreadable." note: using clang-format (always) solves this last issue handily. >"Hard-to-read code usually hides bugs." --- >p270 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p270.cpp && ./a.out main.cpp: In function 'void f1()': main.cpp:28:7: error: a function-definition is not allowed here before '{' token 28 | { | ^ main.cpp:30:8: error: expected primary-expression before '...' token 30 | }... | ^~~ --- >p270 example code https://rentry.org/PPP2_p270 https://coliru.stacked-crooked.com/a/64ccb2b5bea7ba97
Open file (264.31 KB 1140x2287 PPP2_p272.png)
>"Functions are what we use in C++ to name and represent computations and actions." >"A function declaration consists of a return type followed by the name of the function followed by a list of formal arguments in parentheses." >"Formal arguments are often called parameters." >"If you don’t want a function to take arguments, just leave out the formal arguments." --- >p272 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p272.cpp && ./a.out true --- >p272 example code https://rentry.org/PPP2_p272 https://coliru.stacked-crooked.com/a/528e8acf8947333b
Open file (136.59 KB 1140x1662 PPP2_p274.png)
>"We return a value from a function using a return-statement" >"A function declared to return a value must return a value." >"For historical reasons, main() is a special case." >"In a function that does not return a value, we can use return without a value to cause a return from the function." --- >p274 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p274.cpp && ./a.out main.cpp: In function 'double my_abs(int)': main.cpp:29:1: warning: control reaches end of non-void function [-Wreturn-type] 29 | } | ^ main.cpp: In function 'T f()': main.cpp:19:10: warning: 'v' is used uninitialized [-Wuninitialized] 19 | return v; // error: uninitialized return | ^ main.cpp:15:5: note: 'v' was declared here 15 | V v; | ^ main.cpp: In function 'int main()': main.cpp:47:5: warning: 'v' is used uninitialized [-Wuninitialized] 47 | T t(v); // error: v uninitialized when used here | ^ main.cpp:42:5: note: 'v' was declared here 42 | V v; | ^ 00 --- >p274 example code https://rentry.org/PPP2_p274 https://coliru.stacked-crooked.com/a/018e58374eca59b5
Open file (102.29 KB 1140x987 PPP2_p275.png)
>"The simplest way of passing an argument to a function is to give the function a copy of the value you use as the argument." >"An argument of a function f() is a local variable in f() that’s initialized each time f() is called." >"Pass-by-value is pretty straightforward and it's cost is the cost of copying the value." --- >p275 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p275.cpp && ./a.out 1 0 8 7 --- >p275 example code https://rentry.org/PPP2_p275 https://coliru.stacked-crooked.com/a/b31d69f1870fabf5
Open file (128.64 KB 1140x1337 PPP2_p276.png)
>"Pass-by-value is simple, straightforward, and efficient when we pass small values, such as an int, a double, or a Token (§6.3.2). But what if a value is large, such as an image (often, several million bits), a large table of values (say, thousands of integers), or a long string (say, hundreds of characters)?" >"Then, copying can be costly." >"The question we must ask ourselves here is: “Why are we copying anything at all?”" --- >p276 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p276.cpp && ./a.out { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } --- >p276 example code https://rentry.org/PPP2_p197 https://coliru.stacked-crooked.com/a/32a0ef31d7ae6a91
Open file (161.41 KB 1140x1662 PPP2_p277.png)
>>21731 >"As an analogy, if you were given the task to make a list of books in a library, the librarians wouldn’t ship you a copy of the library building and all its contents; they would send you the address of the library, so that you could go and look at the books [there, in-place]." >"Obviously, there has to be a way for us to pass a variable to a function without copying it. Such an “address” is called a reference" >"A const reference [also] has the useful property that we can’t accidentally modify the object passed." >"Pass-by-const-reference is a useful and popular mechanism." --- >p277 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p277.cpp && ./a.out { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } --- >p277 example code https://rentry.org/PPP2_p277 https://coliru.stacked-crooked.com/a/5d90b095ddc569b7
Open file (110.87 KB 1140x1112 PPP2_p279a.png)
>"But what if we did want a function to modify its arguments?" >"Sometimes, that’s a perfectly reasonable thing to wish for." >"[We can avoid copying by still using a reference, but modify the original by passing] a “plain reference” [instead (that is, a non-const ref)]" --- >p279a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p279a.cpp && ./a.out --- >p279a example code https://rentry.org/PPP2_p279a https://coliru.stacked-crooked.com/a/8ed6c65607b5db02
Open file (168.78 KB 1140x1687 PPP2_p279b.png)
>>21733 >"A reference is a construct that allows a user to declare a new name for an object." >"References can be useful as shorthand." >"This key property of references, that a reference can be a convenient shorthand for some object, is what makes them useful as arguments." --- >p279b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p279b.cpp && ./a.out 1 1 8 8 --- >p279b example code https://rentry.org/PPP2_p279b https://coliru.stacked-crooked.com/a/7e1dfeb653f4794e
Open file (99.66 KB 1140x962 PPP2_p281a.png)
>>21734 >"Using references, we can write a function that swaps doubles [directly]" >"Pass-by-reference is clearly a very powerful mechanism" --- >p281a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p281a.cpp && ./a.out x == 1, y == 2 x == 2, y == 1 --- >p281a example code https://rentry.org/PPP2_p281a https://coliru.stacked-crooked.com/a/8c96cbfc62b22436
Open file (223.06 KB 1140x2287 PPP2_p281b.png)
>"When should you use pass-by-value, pass-by-reference, and pass-by-const-reference?" >"If you want to change the value of the object passed, you must use a non-const reference: pass-by-value gives you a copy and pass-by-const-reference prevents you from changing the value of the object passed." >"If we use a reference simply to avoid copying, we use a const reference." >"Consequently, when we see a non-const-reference argument, we assume that the function changes the value of its argument; that is, when we see a pass-by-non-const-reference we assume that not only can that function modify the argument passed, but it will, so that we have to look extra carefully at the call to make sure that it does what we expect it to." --- >p281b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p281b.cpp && ./a.out 0 3 --- >p281b example code https://rentry.org/PPP2_p281b https://coliru.stacked-crooked.com/a/6ae9cd32080b955f
Open file (188.72 KB 1140x1887 PPP2_p284.png)
>"Passing an argument is the initialization of the function’s formal argument with the actual argument specified in the call." >"Conversions are often useful, but occasionally they give surprising results (see §3.9.2). Consequently, we have to be careful with them." >"If you really mean to truncate a double value to an int, say so explicitly" --- >p284 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p284.cpp && ./a.out 3.1415 --- >p284 example code https://rentry.org/PPP2_p284 https://coliru.stacked-crooked.com/a/0e11557938c98da7
Open file (182.79 KB 1140x2362 PPP2_p286.png)
>"When a function is called, the language implementation sets aside a data structure containing a copy of all its parameters and local variables." >"Such a data structure is called a function activation record, and each function has its own detailed layout of its activation record. So, each time we call a function the stack of activation records, usually just called the stack, grows by one record." >''"Conversely, when the function returns, it's [activation] record is no longer used [and gets "popped" from the stack]." >"The stack, also called the call stack, is a data structure that grows and shrinks at one end [usually top-down] according to the rule “Last in, first out.”" --- >p286 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p286.cpp && ./a.out K --- >p286 example code https://rentry.org/PPP2_p286 https://coliru.stacked-crooked.com/a/ac464cea6b39e2a4
Edited last time by Chobitsu on 04/04/2023 (Tue) 17:51:39.
Open file (150.29 KB 1140x1387 PPP2_p290.png)
>"A function represents a calculation, and sometimes we want to do a calculation at compile time." >"The reason to want a calculation to be evaluated by the compiler is usually to avoid having the same calculation done millions of times at run time." >"A constexpr function behaves just like an ordinary function until you use it where a constant is needed. Then, it is calculated at compile time provided its arguments are constant expressions [] and gives an error if they are not" --- >p290 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p290.cpp && ./a.out main.cpp: In function 'void user(Point)': main.cpp:24:32: error: 'p1' is not a constant expression 24 | constexpr Point p5 = scale(p1); | ^ main.cpp:26:32: error: the value of 'p2' is not usable in a constant expression 26 | constexpr Point p6 = scale(p2); // p6 == {100,8} | ^ main.cpp:18:9: note: 'p2' was not declared 'constexpr' 18 | Point p2{10, 10}; | ^~ --- >p290 example code https://rentry.org/PPP2_p290 https://coliru.stacked-crooked.com/a/e48bf1bcb9a7663b
Edited last time by Chobitsu on 02/16/2024 (Fri) 21:34:12.
Open file (121.64 KB 1140x1337 PPP2_p291.png)
>"The evaluation of a program — also called the execution of a program — proceeds through the statements according to the language rules." >"When this “thread of execution” reaches the definition of a variable, the variable is constructed; that is, memory is set aside for the object and the object is initialized." >"When the variable goes out of scope, the variable is destroyed; that is, the object it refers to is in principle removed and the compiler can use its memory for something else." >"Global variables are initialized before the first statement of main() is executed. They “live” until the program terminates, and then they are destroyed." --- >p291 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p291.cpp && ./a.out --- >p291 example code https://rentry.org/PPP2_p291 https://coliru.stacked-crooked.com/a/140c87b4b30681e9
Open file (107.99 KB 1140x1062 PPP2_p292.png)
>"if you change the value of a variable in an expression, don’t read or write it twice in that same expression." >"Unfortunately, not all compilers warn if you write such bad code; it’s bad because you can’t rely on the results being the same if you move your code to another computer, use a different compiler, or use a different optimizer setting." >"Note in particular that = (assignment) is considered just another operator in an expression, so there is no guarantee that the left-hand side of an assignment is evaluated before the right-hand side." >"That’s why v[++i] = i is undefined." --- >p292 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p292.cpp && ./a.out main.cpp: In function 'int main()': main.cpp:19:12: warning: operation on 'i' may be undefined [-Wsequence-point] 19 | v[i] = ++i; // don't: undefined order of evaluation | ^~~ main.cpp:20:5: warning: operation on 'i' may be undefined [-Wsequence-point] 20 | v[++i] = i; // don't: undefined order of evaluation | ^~~ main.cpp:20:5: warning: operation on 'i' may be undefined [-Wsequence-point] main.cpp:21:12: warning: operation on 'i' may be undefined [-Wsequence-point] 21 | int x = ++i + ++i; // don't: undefined order of evaluation | ^~~ main.cpp:22:11: warning: operation on 'i' may be undefined [-Wsequence-point] 22 | cout << ++i << ' ' << i << '\n'; // don't: undefined order of evaluation | ^~~ main.cpp:22:11: warning: operation on 'i' may be undefined [-Wsequence-point] main.cpp:23:10: warning: operation on 'i' may be undefined [-Wsequence-point] 23 | f(++i, ++i); // don't: undefined order of evaluation | ^~~ main.cpp:23:10: warning: operation on 'i' may be undefined [-Wsequence-point] 9 9 11 11 16 --- >p292 example code https://rentry.org/PPP2_p292 https://coliru.stacked-crooked.com/a/1e0e7f3fce17484d
Open file (169.41 KB 1140x1737 PPP2_p293.png)
>"Global variables (and namespace variables; see §8.7) in a single translation unit are initialized in the order in which they appear." >"Using a global variable in anything but the most limited circumstances is usually not a good idea." >"Avoid such code, and be very suspicious when you see global variables with nontrivial initializers; consider any initializer that isn’t a constant expression complicated [(i.e., nontrivial)]." --- >p293 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p293.cpp && ./a.out 197019701970 --- >p293 example code https://rentry.org/PPP2_p293 https://coliru.stacked-crooked.com/a/f03f85f353083584
Open file (113.96 KB 1140x1237 PPP2_p295.png)
>"The language mechanism for [] grouping of declarations is a namespace." >"[When you name something foo, bar, etc.,] most likely somebody else in the world has used those names; but now that doesn’t matter." >"A name composed of a namespace name (or a class name) and a member name combined by :: is called a fully qualified name." --- >p295 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p295.cpp && ./a.out --- >p295 example code https://rentry.org/PPP2_p295 https://coliru.stacked-crooked.com/a/6c0e66061b7bf826
Open file (83.72 KB 1140x737 PPP2_p296a.png)
>"Writing fully qualified names [each and every time] can be tedious." --- >p296a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p296a.cpp && ./a.out Please enter your first name Hello, --- >p296a example code https://rentry.org/PPP2_p296a https://coliru.stacked-crooked.com/a/70e64a3c59f0e350
Open file (107.28 KB 1140x887 PPP2_p296b.png)
>>21744 >"Having seen the standard library string and cout thousands of times, we don’t really want to have to refer to them by their “proper” fully qualified names std::string and std::cout all the time." >"A solution is to say that “by string, I mean std::string,” “by cout, I mean std::cout,” etc." >"That construct is called a using declaration" --- >p296b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p296b.cpp && ./a.out Please enter your first name Hello, --- >p296b example code https://rentry.org/PPP2_p296b https://coliru.stacked-crooked.com/a/3fa4fe2b1b6b52f7
Open file (91.06 KB 1140x762 PPP2_p296c.png)
>>21745 >"Sometimes, we prefer an even stronger “shorthand” for the use of names from a namespace: “If you don’t find a declaration for a name in this scope, look in std.”" >"The way to say that is to use a using directive" --- >p296c command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p296c.cpp && ./a.out Please enter your first name Hello, --- >p296c example code https://rentry.org/PPP2_p296c https://coliru.stacked-crooked.com/a/4d60c9e0825dd407
Open file (82.39 KB 1140x837 PPP2_p297.png)
>>21746 >"It is usually a good idea to avoid using directives for any namespace except for a namespace, such as std, that’s extremely well known in an application area." >"The problem with overuse of using directives [as opposed to using declarations, which are good] is that you lose track of which names come from where, so that you again start to get name clashes." >"However, to simplify our initial code we did place a using directive for std in std_lib_facilities.h." Stop You are done with this chapter, and congratulations. There were a lot of technicalities involved here, and you owe yourself a reward Anon! :^) --- >p297 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out Please enter your first name Hello, --- >p297 example code https://rentry.org/PPP2_p297 https://coliru.stacked-crooked.com/a/e92805f64eba0313
Edited last time by Chobitsu on 04/06/2023 (Thu) 19:06:30.
Open file (91.25 KB 1140x987 PPP2_p305.png)
>"A class is a user-defined type. It is composed of built-in types, other user-defined types, and functions. The parts used to define the class are called members. A class has zero or more members." >"Members can be of various types. Most are either data members, which define the representation of an object of the class, or function members, which provide operations on such objects." >"We access members using the object.member notation." --- >p305 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p305.cpp && ./a.out --- >p305 example code https://rentry.org/PPP2_p305 https://coliru.stacked-crooked.com/a/ee9a126b211da8bc
Open file (119.43 KB 1140x1037 PPP2_p306.png)
>"Usually, we think of a class as having an interface plus an implementation. The interface is the part of the class’s declaration that its users access directly." >"The implementation is that part of the class’s declaration that its users access only indirectly through the interface." >"The public interface is identified by the label public: and the implementation by the label private:." --- >p306 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p306.cpp && ./a.out --- >p306 example code https://rentry.org/PPP2_p306 https://coliru.stacked-crooked.com/a/9fcceec1b329b531
Open file (110.51 KB 1140x1187 PPP2_p307.png)
>>21781 >"A user cannot directly refer to a private member. Instead, we have to go through a public function that can use it." >"We use private and public to represent the important distinction between an interface (the user’s view of the class) and implementation details (the implementer’s view of the class)." >"for something that’s just data, this distinction doesn’t make sense. So, there is a useful simplified notation for a class that has no private implementation details. A struct is a class where members are public by default" --- >p307 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p307.cpp && ./a.out --- >p307 example code https://rentry.org/PPP2_p307 https://coliru.stacked-crooked.com/a/2da06b285f2dbb11
Open file (212.76 KB 1140x2087 PPP2_p308.png)
>"Let’s illustrate the language facilities supporting classes and the basic techniques for using them by showing how — and why — we might evolve a simple data structure into a class with private implementation details and supporting operations." >"We use the apparently trivial problem of how to represent a date (such as August 14, 1954) in a program." >"How would we represent a date?" >"A Date object, such as today, will simply be three ints. There is no “magic” relying on hidden data structures anywhere related to a Date" >"The snag is that nothing is really convenient. Just about anything that we want to do with a Date has to be written in terms of reads and writes of those members. This is tedious and error-prone. Did you spot the error? Everything that’s tedious is error-prone!" >"What we do then is to provide some helper functions to do the most common operations for us." --- >p308 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p308.cpp && ./a.out 2000200011 200028 --- >p308 example code https://rentry.org/PPP2_p308 https://coliru.stacked-crooked.com/a/e18fee457c284d39
Open file (171.86 KB 1140x1762 PPP2_p310.png)
>"Whenever we define a type, we want some operations for it. Exactly how many operations we want and of which kind will vary." >"Exactly how we provide them (as functions, member functions, or operators) will also vary, but whenever we decide to provide a type, we ask ourselves, “Which operations would we like for this type?”" >"We provided an initialization function for Dates, one that provided an important check on the validity of Dates. However, checking functions are of little use if we fail to use them." >"This [] leads to a demand for an initialization function that can’t be forgotten and for operations that are less likely to be overlooked." --- >p310 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p310.cpp && ./a.out 012242005 --- >p310 example code https://rentry.org/PPP2_p310 https://coliru.stacked-crooked.com/a/987972682f570377
Open file (181.04 KB 1140x1412 PPP2_p311.png)
>"A member function with the same name as its class is special. It is called a constructor and will be used for initialization (“construction”) of objects of the class." >"It is an error — caught by the compiler — to forget to initialize an object of a class that has a constructor that requires an argument, and there is a special convenient syntax for doing such initialization:" >"a { } list immediately after the name of the variable [is] the most common style of initialization of variables of a class that has a constructor requiring arguments." --- >p311 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p311.cpp && ./a.out --- >p311 example code https://rentry.org/PPP2_p311 https://coliru.stacked-crooked.com/a/07ca2db23401faf8
Open file (129.38 KB 1140x1062 PPP2_p312.png)
>"We still have a problem: What if someone forgets to use the member function add_day()? What if someone decides to change the month directly?" >"As long as we leave the representation of Date accessible to everybody, somebody will — by accident or design — mess it up; that is, someone will do something that produces an invalid value." >"Such invalid objects are time bombs; it is just a matter of time before someone innocently uses the invalid value and gets a run-time error or — usually worse — produces a bad result." --- >p312 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p312.cpp && ./a.out --- >p312 example code https://rentry.org/PPP2_p312 https://coliru.stacked-crooked.com/a/3e643015671d6f2d
Open file (125.92 KB 1140x1162 PPP2_p313.png)
>>21786 >"Such concerns lead us to conclude that the representation of Date should be inaccessible to users except through the public member functions that we supply." >"We try to design our types so that values are guaranteed to be valid; that is, we hide the representation, provide a constructor that creates only valid objects, and design all member functions to expect valid values and leave only valid values behind when they return." >"A rule for what constitutes a valid value is called an invariant. The invariant for Date (“A Date must represent a day in the past, present, or future”) is unusually hard to state precisely: remember leap years, the Gregorian calendar, time zones, etc." >"If we can’t think of a good invariant, we are probably dealing with plain data. If so, use a struct." --- >p313 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p313.cpp && ./a.out 12 --- >p313 example code https://rentry.org/PPP2_p313 https://coliru.stacked-crooked.com/a/9a87e8754194c3cc
Open file (175.54 KB 1140x1762 PPP2_p314.png)
>"here is a subset of the Date class reorganized to suit the common style of providing the public interface first" >"People put the public interface first because the interface is what most people are interested in. In principle, a user need not look at the implementation details." >"In reality, we are typically curious and have a quick look to see if the implementation looks reasonable and if the implementer used some technique that we could learn from." >"The compiler doesn’t care about the order of class function and data members; it takes the declarations in any order you care to present them." --- >p314 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out main.cpp: In function 'int month()': main.cpp:35:10: error: 'm' was not declared in this scope 35 | return m; // not the member function, can’t access m | ^ --- >p314 example code https://rentry.org/PPP2_p314 https://coliru.stacked-crooked.com/a/4972efb729362694
Open file (134.47 KB 1140x1312 PPP2_p315.png)
>>21788 >"The :y{yy}, m{mm}, d{dd} notation is how we initialize members. It is called a (member) initializer list." >"[We could default initialize members, but that opens up] the possibility of accidentally using a member before it was initialized." >"We can also define member functions right in the class definition" --- >p315 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p315.cpp && ./a.out 2004829 --- >p315 example code https://rentry.org/PPP2_p315/png https://coliru.stacked-crooked.com/a/3c91ba51c2f1249c
Open file (108.24 KB 1140x1312 PPP2_p317a.png)
>"How does Date::month() know to return the value of d1.m in the first call and d2.m in the second?" >"How does Date::month() know for which object it was called?" >"A class member function, such as Date::month(), has an implicit argument which it uses to identify the object for which it is called." --- >p317a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p317a.cpp && ./a.out 3 2 --- >p317a example code https://rentry.org/PPP2_p317a https://coliru.stacked-crooked.com/a/bb2ead549d27084a
Open file (229.71 KB 1140x2312 PPP2_p317b.png)
>"What do we do when we find an invalid date? Where in the code do we look for invalid dates?" >"From §5.6, we know that the answer to the first question is “Throw an exception,” and the obvious place to look is where we first construct a Date." >"If we don’t create invalid Dates and also write our member functions correctly, we will never have a Date with an invalid value." --- >p317b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p317b.cpp && ./a.out {2004,8,29} error: invalid date --- >p317b example code https://rentry.org/PPP2_p317b https://coliru.stacked-crooked.com/a/cf3ffc0450ef23f5
Open file (185.83 KB 1140x2212 PPP2_p318.png)
>"An enum (an enumeration) is a very simple user-defined type, specifying its set of values (its enumerators) as symbolic constants. The “body” of an enumeration is simply a list of its enumerators." >"The class in enum class means that the enumerators are in the scope of the enumeration. That is, to refer to jan, we have to say Month::jan." >"If we don’t initialize the first enumerator, the count starts with 0." --- >p318 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p318.cpp && ./a.out error: bad month --- >p318 example code https://rentry.org/PPP2_p318 https://coliru.stacked-crooked.com/a/58d359c9b9b58e59
Open file (231.81 KB 1140x2412 PPP2_p320.png)
>"In addition to the enum classes, also known as scoped enumerations, there are “plain” enumerations that differ from scoped enumerations by implicitly “exporting” their enumerators to the scope of the enumeration and allowing implicit conversions to int." >"Obviously, “plain” enums are less strict than enum classes. Their enumerators can “pollute” the scope in which their enumerator is defined." >"Prefer the simpler and safer enum classes to “plain” enums, but expect to find “plain” enums in older code" --- >p320 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p320.cpp && ./a.out error: bad month --- >p320 example code https://rentry.org/PPP2_p320 https://coliru.stacked-crooked.com/a/defdf2fb1b8fcfdd
Open file (175.31 KB 1140x1887 PPP2_p321.png)
>"You can define almost all C++ operators for class or enumeration operands." >"That’s often called operator overloading. We use it when we want to provide conventional notation for a type we design." >"You can define just about any operator provided by C++ for your own types, but only existing operators, such as +, –, *, /, %, [ ], ( ), ^, !, &, <, <=, >, and >=." --- >p321 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p321.cpp && ./a.out January --- >p321 example code https://rentry.org/PPP2_p321 https://coliru.stacked-crooked.com/a/9d1fdb263b5790f6
Open file (164.73 KB 1140x1612 PPP2_p324.png)
>"When we defined the constructor for Date in §9.4.3, we used three ints as the arguments. That caused some problems" >"When we use a Month type, the compiler will catch us if we swap month and day, and using an enumeration as the Month type also gives us symbolic names to use." >"When we have a choice, we catch errors at compile time rather than at run time. We prefer for the compiler to find the error rather than for us to try to figure out exactly where in the code a problem occurred." --- >p324 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p324.cpp && ./a.out --- >p324 example code https://rentry.org/PPP2_p324 https://coliru.stacked-crooked.com/a/ec9638833510d62e
Open file (223.83 KB 1140x2362 PPP2_p325.png)
>"Also, errors caught at compile time don’t require checking code to be written and executed." >"When we program, we always have to ask ourselves what is good enough for a given application. We usually don’t have the luxury of being able to search “forever” for the perfect solution after we have already found one that is good enough." >"Search further, and we might even come up with something that’s so elaborate that it is worse than the simple early solution. This is one meaning of the saying “The best is the enemy of the good” (Voltaire)." --- >p325 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p325.cpp && ./a.out error: Invalid year --- >p325 example code https://rentry.org/PPP2_p325 https://coliru.stacked-crooked.com/a/3aaafcec8caba289
Open file (146.67 KB 1140x1712 PPP2_p326.png)
>"Can we copy our [user-defined] objects? And if so, how do we copy them?" >"For Date or Month, the answer is that we obviously want to copy objects of that type and that the meaning of copy is trivial: just copy all of the members." >"What if we don’t want the default meaning of copying? We can either define our own (see §18.3) or delete the copy constructor and copy assignment (see §14.2.4)." --- >p326 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p326.cpp && ./a.out {1978,12,24} --- >p326 example code https://rentry.org/PPP2_p326 https://coliru.stacked-crooked.com/a/9a59904baf0c8397
Open file (282.14 KB 1140x2512 PPP2_p327.png)
>"Uninitialized variables can be a serious source of errors. To counter that problem, we have the notion of a constructor to guarantee that every object of a class is initialized." >"For example, we declared the constructor Date::Date(int,Month,int) to ensure that every Date is properly initialized. In the case of Date, that means that the programmer must supply three arguments of the right types." >"Many classes have a good notion of a default value; that is, there is an obvious answer to the question “What value should it have if I didn’t give it an initializer?”" --- >p327 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p327.cpp && ./a.out --- >p327 example code https://rentry.org/PPP2_p327 https://coliru.stacked-crooked.com/a/ff15abfd80456558
Open file (122.61 KB 1140x1537 PPP2_p328.png)
>>21798 >"Using a default constructor is not just a matter of looks. Imagine that we could have an uninitialized string or vector." >"If the values of s and v were genuinely undefined, s and v would have no notion of how many elements they contained or (using the common implementation techniques; see §17.5) where those elements were supposed to be stored." >"For many types, it is better to define a constructor that gives meaning to the creation of an object without an explicit initializer. Such a constructor takes no arguments and is called a default constructor." --- >p328 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p328.cpp && ./a.out --- >p328 example code https://rentry.org/PPP2_p328 https://coliru.stacked-crooked.com/a/4d85e414c11129dd
Open file (332.00 KB 1140x3312 PPP2_p329.png)
>>21799 >"Instead of placing the default values for members in the constructor, we could place them on the members themselves, That way, the default values are available to every constructor." >"the specified initializers (Month::jan and 1) are implicitly used. An initializer for a class member specified as part of the member declaration is called an in-class initializer." >"If we didn’t like to build the default value right into the constructor code, we could use a constant (or a variable). To avoid a global variable and its associated initialization problems, we use the technique from §8.6.2" >"Note that the default constructor does not need to check its value; the constructor for default_date() already did that." --- >p329 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p329.cpp && ./a.out {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} {2001,1,1} --- >p329 example code https://rentry.org/PPP2_p329 https://coliru.stacked-crooked.com/a/f37c081c07c1f615
Open file (225.15 KB 1140x2162 PPP2_p330.png)
>"Some variables are meant to be changed — that’s why we call them “variables” — but some are not; that is, we have “variables” representing immutable values. Those, we typically call constants or just consts." >"[we can classify] operations on a class as modifying and nonmodifying (that is, const or not)." >"That’s a pretty fundamental distinction that helps us understand a class, but it also has a very practical importance: operations that do not modify the object can be invoked for const objects." >"What the compiler provides for the class implementer is primarily protection against accident, which is particularly useful for more complex code." --- >p330 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_09/main_p330.cpp && ./a.out 20 — 21 --- >p330 example code https://rentry.org/PPP2_p330 https://coliru.stacked-crooked.com/a/28aae79240d5304d
Open file (315.61 KB 1140x3812 PPP2_p332.png)
>"When we design our interfaces to be minimal (though complete), we have to leave out lots of operations that are merely useful. A function that can be simply, elegantly, and efficiently implemented as a freestanding function (that is, as a nonmember function) should be implemented outside the class." >"That way, a bug in that function cannot directly corrupt the data in a class object. Not accessing the representation is important because the usual debug technique is “Round up the usual suspects”; that is, when something goes wrong with a class, we first look at the functions that directly access the representation: one of those almost certainly did it." >"If there are a dozen such functions, we will be much happier than if there were 50." >"Note also that if the representation changes, only the functions that directly access the representation need to be rewritten. That’s another strong practical reason for keeping interfaces minimal." --- >p332 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic main.cpp && ./a.out --- >p332 example code https://rentry.org/PPP2_p332 https://coliru.stacked-crooked.com/a/495f58f3450f144e
Edited last time by Chobitsu on 04/06/2023 (Thu) 22:47:17.
Open file (129.35 KB 1140x1215 PPP2_p334.png)
>"So, let’s just put it all together and see what that Date class might look like when we combine all of the ideas/concerns." >"-First we place the declarations in a header Chrono.h >-The definitions go into Chrono.cpp" note: we also added 'chrono_helper' files to help further separate concerns, and ease maintenance. Good job. You've completed this chapter, and are well on the way to understanding how to create your own custom, sophisticated, user-defined types in C++. Congrats Anon, this skill will prove exceptionally valuable to you while creating the systems software that runs your robowaifus! :^) --- >p334 command line + possible output: ln -s /Archive2/a5/a5e601f04de3e8/main.cpp Chrono.h ln -s /Archive2/5d/ba19e9a091d9fc/main.cpp Chrono.cpp ln -s /Archive2/54/6a1d31db214a2b/main.cpp chrono_helper.h ln -s /Archive2/af/7eee19adf635c5/main.cpp chrono_helper.cpp g++ -std=c++20 -O2 -Wall -pedantic -pthread chrono_helper.cpp Chrono.cpp main.cpp && ./a.out holiday is {1978,7,4} d2 is {1978,7,11} Sunday --- >p334 example code https://rentry.org/PPP2_p334 https://rentry.org/PPP2_Chrono_h https://rentry.org/PPP2_Chrono_cpp https://rentry.org/PPP2_chrono_helper_h https://rentry.org/PPP2_chrono_helper_cpp https://coliru.stacked-crooked.com/a/9f7924b9c4997882
Edited last time by Chobitsu on 04/07/2023 (Fri) 10:19:03.
Open file (191.26 KB 1140x2091 PPP2_Chrono_h.png)
Open file (108.12 KB 1140x941 PPP2_chrono_helper_h.png)
Open file (103.82 KB 1140x1216 PPP2_Chrono_cpp.png)
Open file (317.65 KB 1140x3516 PPP2_chrono_helper_cpp.png)
Open file (193.31 KB 1140x2037 PPP2_p350.png)
>"Without data, computing is pointless. We need to get data into our program to do interesting computations and we need to get the results out again." >"the [C++ standard] I/O library provides an abstraction of I/O so that the programmer doesn’t have to think about devices and device drivers" >"In this chapter and the next, we’ll see how I/O consisting of streams of formatted data is done using the C++ standard library." >"If you want to read from a file or write to a file you have to open a stream specifically for that file. An ifstream is an istream for reading from a file, an ofstream is an ostream for writing to a file" >"Before a file stream can be used it must be attached to a file." note: sample input file 'points.txt' included with the code archive (in OP). >"Defining an ifstream with a name string opens the file of that name for reading." --- >p350 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p350.cpp && ./a.out Please enter input file name (eg, points.txt ): error: can't open input file --- >p350 example code https://rentry.org/PPP2_p350 https://coliru.stacked-crooked.com/a/9592f24af2c65c4c
Edited last time by Chobitsu on 04/09/2023 (Sun) 18:15:32.
Open file (236.20 KB 1140x2437 PPP2_p351.png)
>>21816 >"Defining an ofstream with a name string opens the file with that name for writing. The test of !ost checks if the file was properly opened." >"When a file stream goes out of scope its associated file is closed. When a file is closed its associated buffer is “flushed”; that is, the characters from the buffer are written to the file." >"It is usually best to open files early in a program before any serious computation has taken place. After all, it is a waste to do a lot of work just to find that we can’t complete it because we don’t have anywhere to write our results." >"Opening the file implicitly as part of the creation of an ostream or an istream and relying on the scope of the stream to take care of closing the file is the ideal." --- >p351 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p351.cpp && ./a.out Please enter name of output file: error: can't open output file --- >p351 example code https://rentry.org/PPP2_p351 https://coliru.stacked-crooked.com/a/c7def500bda04b32
Open file (174.10 KB 1140x1662 PPP2_p352.png)
>>21817 >"You can also perform explicit open() and close() operations (§B.7.1)." >"However, relying on scope minimizes the chances of someone trying to use a file stream before it has been attached to a stream or after it was closed. Don’t forget to test a stream after opening it." >"Why would you use open() or close() explicitly? Well, occasionally the lifetime of a connection to a file isn’t conveniently limited by a scope so you have to." >"But that’s rare enough for us not to have to worry about it here. More to the point, you’ll find such use in code written by people using styles from languages and libraries that don’t have the scoped idiom used by iostreams (and the rest of the C++ standard library)." --- >p352 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p352.cpp && ./a.out error: impossible --- >p352 example code https://rentry.org/PPP2_p352 https://coliru.stacked-crooked.com/a/08e57632fb3bd276
Open file (283.64 KB 1140x2487 PPP2_p353.png)
>"Consider how you might read a set of results of some measurements from a file and represent them in memory. These might be the temperature readings from a weather station" >"The istream called ist could be an input file stream (ifstream) as shown in the previous section, (an alias for) the standard input stream (cin), or any other kind of istream." >"For code like this, it doesn’t matter exactly from where the istream gets its data. All that our program cares about is that ist is an istream and that the data has the expected format." note: sample input file 'temps.txt' included with the code archive (in OP). >"Writing to a file is usually simpler than reading from one. Again, once a stream is initialized we don’t have to know exactly what kind of stream it is." --- >p353 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p353.cpp && ./a.out Please enter input file name (eg, temps.txt ): error: can't open input file --- >p353 example code https://rentry.org/PPP2_p353 https://coliru.stacked-crooked.com/a/73961ba23af76e23
Edited last time by Chobitsu on 04/09/2023 (Sun) 18:16:16.
Open file (158.32 KB 1140x1562 PPP2_p355.png)
>"When dealing with input we must expect errors and deal with them." >''"The possibilities for input errors are limitless! However, an istream reduces all to four possible cases, called the stream state:"'' >- good() The operations succeeded. >- eof() We hit end of input (“end of file”). >- fail() Something unexpected happened (e.g., we looked for a digit and found 'x'). >- bad() Something unexpected and serious happened (e.g., a disk read error). --- >p355 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p355.cpp && ./a.out --- >p355 example code https://rentry.org/PPP2_p355 https://coliru.stacked-crooked.com/a/afb6a564e2c2a036
Open file (204.08 KB 1140x1787 PPP2_p356.png)
>>21820 >"Consider how to read a sequence of integers that may be terminated by the character * or an “end of file” (Ctrl+Z on Windows, Ctrl+D on Unix) into a vector." >"Note that when we didn’t find the terminator, we still returned. After all, we may have collected some data and the caller of fill_vector() may be able to recover from a fail()." >"We put the character back into ist using unget(); the caller of fill_vector() might have a use for it." >"If you called fill_vector() and want to know what terminated the read, you can test for fail() and eof()." --- >p356 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p356.cpp && ./a.out --- >p356 example code https://rentry.org/PPP2_p356 https://coliru.stacked-crooked.com/a/46a85f9b5ddee9b0
Open file (193.92 KB 1140x1687 PPP2_p357.png)
>>21821 >"in almost all cases the only thing we want to do if we encounter bad() is to throw an exception." >"To make life easier, we can tell an istream to do that for us:" >"You can test an ostream for exactly the same states as an istream: good(), fail(), eof(), and bad()." --- >p357 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p357.cpp && ./a.out --- >p357 example code https://rentry.org/PPP2_p357 https://coliru.stacked-crooked.com/a/0f652935447421aa
Open file (102.93 KB 1140x962 PPP2_p358a.png)
>"So, we know how to read a series of values ending with the end of file or a terminator." >"let’s just have a look at the ever popular idea of repeatedly asking for a value until an acceptable one is entered." >"This example will allow us to examine several common design choices." --- >p358a command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p358a.cpp && ./a.out Please enter an integer in the range 1 to 10 (inclusive): --- >p358a example code https://rentry.org/PPP2_p358a https://coliru.stacked-crooked.com/a/f71152e1cdd31ee8
Open file (94.42 KB 1140x887 PPP2_p358b.png)
>>21823 >"to get a robust read we have to deal with three problems:" >1. The user typing an out-of-range value >2. Getting no value (end of file) >3. The user typing something of the wrong type (here, not an integer) >"What do we want to do in those three cases? That’s often the question when writing a program: What do we really want? Here, for each of those three errors, we have three alternatives:" >1. Handle the problem in the code doing the read. >2. Throw an exception to let someone else handle the problem (potentially terminating the program). >3. Ignore the problem. >''" [The third choice isn't a legitimate one in production code, so] the choice is not between throwing exceptions and handling problems locally, but [rather] a choice of which problems (if any) we should [(or even can)] handle locally." --- >p358b command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p358b.cpp && ./a.out Please enter an integer in the range 1 to 10 (inclusive): --- >p358b example code https://rentry.org/PPP2_p358b https://coliru.stacked-crooked.com/a/e63b4a5474a4926d
Open file (214.49 KB 1140x1937 PPP2_p359.png)
>"Let’s try handling both an out-of-range input and an input of the wrong type locally" >"This is messy, and rather long-winded. In fact, it is so messy that we could not recommend that people write such code each time they needed an integer from a user." >"On the other hand, we do need to deal with the potential errors because people do make them, so what can we do?" --- >p359 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p359.cpp && ./a.out Please enter an integer in the range 1 to 10 (inclusive): Sorry, that was not a number; please try again error: no input --- >p359 example code https://rentry.org/PPP2_p359 https://coliru.stacked-crooked.com/a/4695ed64a2834c4c
Open file (209.79 KB 1140x2037 PPP2_p360.png)
>>21825 >"The reason that the code is messy is that code dealing with several different concerns is all mixed together:" >• Reading values >• Prompting the user for input >• Writing error messages >• Skipping past “bad” input characters >• Testing the input against a range >"The way to make code clearer is often to separate logically distinct concerns into separate functions." --- >360 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p360.cpp && ./a.out Please enter an integer in the range 1 to 10 (inclusive): Sorry, that was not a number; please try again error: no input --- >360 example code https://rentry.org/PPP2_p360 https://coliru.stacked-crooked.com/a/517eaa8ebc06383f
Open file (237.47 KB 1140x2437 PPP2_p361.png)
>>21826 >"This code is better, but it is still too long and too messy to use many times in a program. We’d never get it consistently right, except after (too) much testing." >"One plausible answer is “a function that reads an int, any int, and another that reads an int of a given range”. If we had those, we would at least be able to use them simply and correctly." >"Don’t forget to catch exceptions somewhere" --- >p361 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p361.cpp && ./a.out Please enter an integer in the range 1 to 10 (inclusive): Sorry, that was not a number; please try again error: no input --- >p361 example code https://rentry.org/PPP2_p361 https://coliru.stacked-crooked.com/a/723e0573ddde99c8
Open file (237.57 KB 1140x2312 PPP2_p363.png)
>"The get_int() functions still mix up reading with writing messages to the user. That’s probably good enough for a simple program, but in a large program we might want to vary the messages written to the user." >"[Also,] “utility functions” that we use in many parts of a program shouldn’t have messages “hardwired” into them." >"Further, library functions that are meant for use in many programs shouldn’t write to the user at all — after all, the library writer may not even know that the program in which the library runs is used on a machine with a human watching." --- >p363 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p363.cpp && ./a.out enter strength: [1:10] Sorry, that was not a number; please try again error: no input --- >p363 example code https://rentry.org/PPP2_p363 https://coliru.stacked-crooked.com/a/af79843733b5a7e7
Open file (159.81 KB 1140x1612 PPP2_p364.png)
>"Defining the output operator, <<, for a given type is typically trivial. The main design problem is that different people might prefer the output to look different, so it is hard to agree on a single format." >"However, even if no single output format is good enough for all uses, it is often a good idea to define << for a user-defined type. That way, we can at least trivially write out objects of the type during debugging and early development." >"Note how operator<<() takes an ostream& as its first argument and returns it again as its return value. That’s the way the output stream is passed along so that you can “chain” output operations." --- >p364 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p364.cpp && ./a.out (1994,3,29) (1994,3,29) (1994,3,29)(2000,2,15) (1994,3,29)(2000,2,15) --- >p364 example code https://rentry.org/PPP2_p364 https://coliru.stacked-crooked.com/a/8f850d8cb4b3f30a
Open file (169.00 KB 1140x1887 PPP2_p365.png)
>"Defining the input operator, >>, for a given type and input format is basically an exercise in error handling. It can therefore be quite tricky." >"As ever, input is harder to deal with than output. There is simply more that can — and often does — go wrong with input than with output." >"Leaving the target Date unchanged in case of a failure to read is the ideal; it tends to lead to cleaner code." --- >p365 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p365.cpp && ./a.out Please enter input date (eg, (1994,3,29) ): date input was (32765,0,0) --- >p365 example code https://rentry.org/PPP2_p365 https://coliru.stacked-crooked.com/a/15e8a3698406f986
Open file (315.17 KB 1140x2837 PPP2_p366.png)
>"we often want to check our reads as we go along [inside our input loop]." >"we read a sequence of values into variables and when we can’t read any more values, we check the stream state to see why." >"We could also decide to designate a character as a terminator" >"The end_of_loop() does nothing unless the stream is in the fail() state. We consider that simple enough and general enough for many purposes." --- >p366 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p366.cpp && ./a.out error: bad termination of input --- >p366 example code https://rentry.org/PPP2_p366 https://coliru.stacked-crooked.com/a/838f49ec7267d4a0
Open file (158.22 KB 1140x1412 PPP2_p369.png)
>"Let’s try to use this “standard loop” for a concrete example. As usual, we’ll use the example to illustrate widely applicable design and programming techniques." >"Assume that you have a file of temperature readings that has been [very loosely] structured" >"How should we represent this data in memory? The obvious first choice is three classes, Year, Month, and Reading, to exactly match the input." --- >p369 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p369.cpp && ./a.out 12 32 24 --- >p369 example code https://rentry.org/PPP2_p369 https://coliru.stacked-crooked.com/a/6dd6e1f6b8588a2a
Edited last time by Chobitsu on 04/09/2023 (Sun) 18:18:00.
Open file (775.88 KB 1140x7690 PPP2_p370.png)
>"The Reading class will be used only for reading input" >"Basically, we check if the format begins plausibly, and if it doesn’t we set the file state to fail() and return. This allows us to try to read the information in some other way." >"Month’s >> does a quick check that a Reading is plausible before storing it" >"Finally, we can read Years. Year’s >> is similar to Month’s >>" note: sample input file 'odd_format.txt' included with the code archive (in OP). --- >p370 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p370.cpp && ./a.out Please enter input file name (eg, odd_format.txt ): error: can't open input file --- >p370 example code https://rentry.org/PPP2_p370 https://coliru.stacked-crooked.com/a/56a2b1ef9d35be5b
Edited last time by Chobitsu on 04/09/2023 (Sun) 18:18:37.
Open file (234.09 KB 1140x2037 PPP2_p375.png)
>"To get Month’s >> to work, we need to provide a way of reading symbolic representations of the month. For symmetry, we’ll provide a matching write using a symbolic representation. We decided to represent the input representation as a vector<string> plus an initialization function and a lookup function" >"When we want to produce output, we have the opposite problem. We have an int representing a month and would like a symbolic representation to be printed. Our solution is fundamentally similar, but instead of using a table to go from string to int, we use one to go from int to string" >"Reading data is fundamental. Writing loops correctly ([e.g.,] initializing every variable [that is] used correctly) is fundamental. Converting between representations is fundamental." --- >p375 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p375.cpp && ./a.out jan's index: 0 feb's index: 1 dec's index: 11 month index 0's print format: January month index 1's print format: February month index 11's print format: December --- >p375 example code https://rentry.org/PPP2_p375 https://coliru.stacked-crooked.com/a/fd0e2981cbd755dd
Open file (1.11 MB 1140x10740 PPP2_p370_v2.png)
>>21833 >"this is a reworked version of the book's original for this section. the primary modification being to use token parsing (similar to chs 6 & 7) instead for retrieving the data from the readings file. one advantage of this new design is that overall memory consumption is lower than with the original." You've finished chapter 10. Congratulations, you've begun working with I/O streams in a flexible manner. This is important for all sorts of data communications both inside & outside your robowaifus. --- >p370_v2 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p370_v2.cpp && ./a.out Please enter input file name (eg, odd_format.txt ): error: can't open input file --- >p370_v2 example code https://rentry.org/PPP2_p370_v2 https://coliru.stacked-crooked.com/a/42b49b624cca9313
Edited last time by Chobitsu on 04/08/2023 (Sat) 01:00:38.
Open file (112.20 KB 1140x1262 PPP2_p381.png)
>"The iostream library — the input/output part of the ISO C++ standard library — provides a unified and extensible framework for input and output of text. By “text” we mean just about anything that can be represented as a sequence of characters." >"Thus, when we talk about input and output we can consider the integer 1234 as text because we can write it using the four characters 1, 2, 3, and 4." >"People care a lot about apparently minor details of the output they have to read." >"Integer values can be output as octal (the base-8 number system), decimal (our usual base-10 number system), and hexadecimal (the base-16 number system)." >"Most output uses decimal. Hexadecimal is popular for outputting hardware-related information. The reason is that a hexadecimal digit exactly represents a 4-bit value." >"In summary, the integer output manipulators are:" >- oct use base-8 (octal) notation >- dec use base-10 (decimal) notation >- hex use base-16 (hexadecimal) notation >- showbase prefix 0 for octal and 0x for hexadecimal >- noshowbase don’t use prefixes --- >p381 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p381.cpp && ./a.out 1234 (decimal) 4d2 (hexadecimal) 2322 (octal) 1234 4d2 2322 2322 1234 4d2 2322 1234 0x4d2 02322 1234 1234 1234 --- >p381 example code https://rentry.org/PPP2_p381 https://coliru.stacked-crooked.com/a/b77ff42d1c94f87b
Open file (112.03 KB 1140x1162 PPP2_p383.png)
>>21863 >"By default, >> assumes that numbers use the decimal notation, but you can tell it to read hexadecimal or octal integers" >"oct, dec, and hex “stick” for input, just as they do for output." >"You can get >> to accept and correctly interpret the 0 and 0x prefixes." --- >p383 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p383.cpp && ./a.out <<< '1234 0x4d2 02322 02322' demo integer inputs: 1234 1234 1234 1234 1234 1234 1234 1234 --- >p383 example code https://rentry.org/PPP2_p383 https://coliru.stacked-crooked.com/a/4c9f488b61743bae
Edited last time by Chobitsu on 04/09/2023 (Sun) 07:29:18.
Open file (129.07 KB 1140x1112 PPP2_p384.png)
>"If you deal directly with hardware, you’ll need hexadecimal (or possibly octal) notation." >"Similarly, if you deal with scientific computation, you must deal with the formatting of floating-point values." >"They are handled using iostream manipulators in a manner very similar to that of integer values." >"In summary, the basic floating-point output-formatting manipulators are:" >- fixed use fixed-point notation >- scientific use mantissa and exponent notation; the mantissa is always in the [1:10) range; that is, there is a single nonzero digit before the decimal point >- defaultfloat choose fixed or scientific to give the numerically most accurate representation, within the precision of defaultfloat --- >p384 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p384.cpp && ./a.out 1234.57 (defaultfloat) 1234.567890 (fixed) 1.234568e+03 (scientific) 1.234568e+03 1234.567890 1.234568e+03 1.234568e+03 1234.57 1234.567890 1.234568e+03 --- >p384 example code https://rentry.org/PPP2_p384 https://coliru.stacked-crooked.com/a/4a19e7ede440fecb
Open file (99.17 KB 1140x837 PPP2_p386.png)
>"By default, a floating-point value is printed using six total digits using the defaultfloat format. The most appropriate format is chosen and the number is rounded to give the best approximation that can be printed using only six digits (the default precision for the defaultfloat format)." >"The rounding rule is the usual 4/5 rule: 0 to 4 round down (toward zero) and 5 to 9 round up (away from zero)." >"Note that floating-point formatting applies only to floating-point numbers" >"The precision is defined as:" >- defaultfloat precision is the total number of digits >- scientific precision is the number of digits after the decimal point >- fixed precision is the number of digits after the decimal point --- >p386 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p386.cpp && ./a.out 1234.57 1234.567890 1.234568e+03 1234.6 1234.56789 1.23457e+03 1234.5679 1234.56789000 1.23456789e+03 --- >p386 example code https://rentry.org/PPP2_p386 https://coliru.stacked-crooked.com/a/20aecff49f8f3bf9
Open file (112.56 KB 1140x1087 PPP2_p387.png)
>"Using scientific and fixed formats, a programmer can control exactly how much space a value takes up on output. That’s clearly useful for printing tables, etc." >"The equivalent mechanism for integer values is called fields. You can specify exactly how many character positions an integer value or string value will occupy using the “set field width” manipulator setw()." >"Note that the field width “doesn’t stick. [I.e.,] unless you set the field width immediately before an output operation, the notion of “field” is not used.”" --- >p387 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p387.cpp && ./a.out 123456|123456| 123456|123456| 12345|12345| 12345|12345| 1234.5|1234.5| 1234.5|1234.5| asdfg|asdfg| asdfg|asdfg| --- >p387 example code https://rentry.org/PPP2_p387 https://coliru.stacked-crooked.com/a/e430e015bae2e24a
Open file (144.78 KB 1140x1162 PPP2_p389.png)
>"You can open a file in one of several modes." >"By default, an ifstream opens its file for reading and an ofstream opens its file for writing. That takes care of most common needs." >"you can choose between several alternatives:" >- ios_base::app append (i.e., add to the end of the file) >- ios_base::ate “at end” (open and seek to end) >- ios_base::binary binary mode — beware of system-specific behavior >- ios_base::in for reading >- ios_base::out for writing >- ios_base::trunc truncate file to 0 length >"A file mode is optionally specified after the name of the file." --- >p389 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p389.cpp && ./a.out --- >p389 example code https://rentry.org/PPP2_p389 https://coliru.stacked-crooked.com/a/80bcef6fb09af871
Open file (264.42 KB 1140x2240 PPP2_p391.png)
>"In memory, we can represent the number 123 as an integer value or as a string value." >"In the first case, 123 is stored as a (binary) number in an amount of memory that is the same as for all other ints (4 bytes, that is, 32 bits, on a PC)." >"Had we chosen the value 12345 instead, the same 4 bytes would have been used." >"binary I/O is messy, somewhat complicated, and error-prone." >"However, as programmers we don’t always have the freedom to choose file formats, so occasionally we must use binary I/O simply because that’s the format someone chose for the files we need to read or write." --- >p391 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p391.cpp && ./a.out Please enter input file name error: can't open input file --- >p391 example code https://rentry.org/PPP2_p391 https://coliru.stacked-crooked.com/a/b6bb9ca1a113a4f2
Open file (166.63 KB 1140x1462 PPP2_p393.png)
>"Whenever you can, just read and write files from the beginning to the end. That’s the easiest and least error-prone way." >"Many times, when you feel that you have to make a change to a file, the better solution is to produce a new file containing the change." >"However, if you must, you can use positioning to select a specific place in a file for reading or writing. [But] please be [very] careful: there is next to no run-time error checking when you use positioning." --- >p393 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p393.cpp && ./a.out error: can't open points.txt --- >p393 example code https://rentry.org/PPP2_p393 https://coliru.stacked-crooked.com/a/11d1315a7a61cefd
Open file (285.01 KB 1140x2512 PPP2_p394.png)
>"You can use a string as the source of an istream or the target for an ostream." >"An istream that reads from a string is called an istringstream and an ostream that stores characters written to it in a string is called an ostringstream." >"The stringstreams are generally used when we want to separate actual I/O from processing." --- >p394 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p394.cpp && ./a.out test() error: double format error: twelve point three --- >p394 example code https://rentry.org/PPP2_p394 https://coliru.stacked-crooked.com/a/6987d8034e6c7bb4
Open file (211.88 KB 1140x2262 PPP2_p395.png)
>"A >> operator reads into objects of a given type according to that type’s standard format." >"The standard library istream library also provides facilities for reading individual characters and whole lines." >"What if we wanted to read everything on that line at once and decide how to format it later? That could be done using the function getline()." >"One common reason for wanting to read a whole line is that the definition of whitespace isn’t always appropriate." --- >p395 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p395.cpp && ./a.out > > Dennis Ritchie > --- >p395 example code https://rentry.org/PPP2_p395 https://coliru.stacked-crooked.com/a/ebdb990928f30a5f
Open file (246.17 KB 1140x1762 PPP2_p397.png)
>"Usually, we read integers, floating-point numbers, words, etc. as defined by format conventions. However, we can — and sometimes must — go down a level of abstraction and read individual characters." >"That’s more work, but when we read individual characters, we have full control over what we are doing." >"When we read individual characters, we usually want to classify them: Is this character a digit? Is this character uppercase? And so forth." note: see the code example for the listing of them. --- >p397 command line + possible output: g++ -std=c++20 -O2 -pedantic ./ch_11/main_p397.cpp && ./a.out > --- >p397 example code https://rentry.org/PPP2_p397 https://coliru.stacked-crooked.com/a/c12fc47b5e326104
Open file (139.90 KB 1140x1337 PPP2_p399.png)
>"This section provides a semi-realistic example of the use of iostreams to solve a real problem. When we read strings, words are by default separated by whitespace. Unfortunately, istream doesn’t offer a facility for us to define what characters make up whitespace or in some other way directly change how >> reads a string." >"So, what do we do if we need another definition of whitespace?" >"For most purposes we must treat punctuation just like whitespace. How might we get rid of such punctuation? We could read characters, remove the punctuation characters — or turn them into whitespace — and then read the “cleaned-up” input again" --- >p399 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p399.cpp && ./a.out > --- >p399 example code https://rentry.org/PPP2_p399 https://coliru.stacked-crooked.com/a/de67503d3a55b7a6
Open file (397.32 KB 1140x2987 PPP2_p400.png)
>>21874 >"Unfortunately, the code above is messy and rather special-purpose. What would we do if we had another definition of punctuation?" >"Let’s provide a more general and useful way of removing unwanted characters from an input stream." >"The basic idea is to read words from an ordinary input stream and then treat the user-specified “whitespace” characters as whitespace" >"To become a programmer, you need to read code, and not just carefully polished solutions to educational problems. This is [one such] example [of real code]." >"In another few days or weeks, this will become easy for you to read, and you will be looking at ways to improve the solution." --- >p400 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p400.cpp && ./a.out <<< "There are only two kinds of languages: languages that people complain about, and languages that people don't use." please enter words (ctrl+d to end input) about and are complain don't kinds languages of only people that there two use --- >p400 example code https://rentry.org/PPP2_p400 https://coliru.stacked-crooked.com/a/8c6f67b01636ed58
Edited last time by Chobitsu on 04/10/2023 (Mon) 02:44:48.
Open file (340.11 KB 1140x2565 PPP2_p391_v2.png)
>>21869 >"note: this is an extended & reworked example of the book's original, that demonstrates moving data for both the character & binary formats to/from disk files, and confirming the resultant data are identical." Stop. Congratulations Anon, you've finished with chapter 11; and in fact with a couple of very detailed chapters loaded with information. C++ I/O streams are a bit tricky to master at first, but they are an exceptionally powerful and (nearly-always) elegant approach to I/O management in general. We can't use them everywhere, but when we can they offer us a unified approach to the world of data I/O that otherwise can be quite chaotic! Your time invested in mastering C++ streams is time well-spent Anon. Cheers. :^) --- >p391_v2 command line + possible output: g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p391_v2.cpp && ./a.out -rw-r--r-- 1 2001 2000 20 Apr 9 04:53 ints.bin -rw-r--r-- 1 2001 2000 57 Apr 9 04:53 ints.txt sizeof(char): 1 sizeof(int): 4 sizeof(double): 8 source data (using C++ types): 2147483647 -2147483648 2147483647 -2147483648 2147483647 reconstructed data (from binary file): 2147483647 -2147483648 2147483647 -2147483648 2147483647 in-memory vector sizes match: true --- >p391_v2 example code https://rentry.org/PPP2_p391_v2 https://coliru.stacked-crooked.com/a/26eb71ffe5a890d4

Report/Delete/Moderation Forms
Delete
Report