/robowaifu/ - DIY Robot Wives

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

The canary has FINALLY been updated. -robi

Server software upgrades done, should hopefully keep the feds away. -robi

LynxChan 2.8 update this weekend. I will update all the extensions in the relevant repos as well.

The mail server for Alogs was down for the past few months. If you want to reach out, you can now use admin at this domain.

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)


He was no longer living a dull and mundane life, but one that was full of joy and adventure.


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. note: This is a read-only document in general. If you happen to catch the thread unlocked while it's still under construction, please resist the temptation to reply ITT -- it will get baleeted! For now, just reply in /meta. We'll have a proper classroom thread utilizing this textbook thread at some point before too long. :^) --- mini FAQ - Halp! How do I pass std::cin data into my program when using Coliru? >by redirecting them appended to the Coliru cmd line, followed by a newline'd EOF https://coliru.stacked-crooked.com/a/c2ff182a19a71bb3 --- >unless otherwise-noted, everything ITT is MIT (Expat) licensed >copyright 2023
Edited last time by Chobitsu on 02/01/2023 (Wed) 15:51:07.
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 (>>00000) 4.5 Functions 113 (>>00000) 4.5.1 Why bother with functions? 115 (>>00000) 4.5.2 Function declarations 117 (>>18746) 4.6 vector 117 (>>00000) 4.6.1 Traversing a vector 119 (>>00000) 4.6.2 Growing a vector 119 (>>00000) 4.6.3 A numeric example 120 (>>00000) 4.6.4 A text example 123 (>>00000) 4.7 Language features 125 Chapter 5 Errors 133 (>>00000) 5.1 Introduction 134 (>>00000) 5.2 Sources of errors 136 (>>00000) 5.3 Compile-time errors 136 (>>00000) 5.3.1 Syntax errors 137 (>>00000) 5.3.2 Type errors 138 (>>00000) 5.3.3 Non-errors 139 (>>00000) 5.4 Link-time errors 139 (>>00000) 5.5 Run-time errors 140 (>>00000) 5.5.1 The caller deals with errors 142 (>>00000) 5.5.2 The callee deals with errors 143 (>>00000) 5.5.3 Error reporting 145 (>>00000) 5.6 Exceptions 146 (>>00000) 5.6.1 Bad arguments 147 (>>00000) 5.6.2 Range errors 148 (>>00000) 5.6.3 Bad input 150 (>>00000) 5.6.4 Narrowing errors 153 (>>00000) 5.7 Logic errors 154 (>>00000) 5.8 Estimation 157 (>>00000) 5.9 Debugging 158 (>>00000) 5.9.1 Practical debug advice 159 (>>00000) 5.10 Pre- and post-conditions 163 (>>00000) 5.10.1 Post-conditions 165 (>>00000) 5.11 Testing 166 Chapter 6 Writing a Program 173 (>>00000) 6.1 A problem 174 (>>00000) 6.2 Thinking about the problem 175 (>>00000) 6.2.1 Stages of development 176 (>>00000) 6.2.2 Strategy 176 (>>00000) 6.3 Back to the calculator! 178 (>>00000) 6.3.1 First attempt 179 (>>00000) 6.3.2 Tokens 181 (>>00000) 6.3.3 Implementing tokens 183 (>>00000) 6.3.4 Using tokens 185 (>>00000) 6.3.5 Back to the drawing board 186 (>>00000) 6.4 Grammars 188 (>>00000) 6.4.1 A detour: English grammar 193 (>>00000) 6.4.2 Writing a grammar 194 (>>00000) 6.5 Turning a grammar into code 195 (>>00000) 6.5.1 Implementing grammar rules 196 (>>00000) 6.5.2 Expressions 197 (>>00000) 6.5.3 Terms 200 (>>00000) 6.5.4 Primary expressions 202 (>>00000) 6.6 Trying the first version 203 (>>00000) 6.7 Trying the second version 208 (>>00000) 6.8 Token streams 209 (>>00000) 6.8.1 Implementing Token_stream 211 (>>00000) 6.8.2 Reading tokens 212 (>>00000) 6.8.3 Reading numbers 214 (>>00000) 6.9 Program structure 215 Chapter 7 Completing a Program 221 (>>00000) 7.1 Introduction 222 (>>00000) 7.2 Input and output 222 (>>00000) 7.3 Error handling 224 (>>00000) 7.4 Negative numbers 229 (>>00000) 7.5 Remainder: % 230 (>>00000) 7.6 Cleaning up the code 232 (>>00000) 7.6.1 Symbolic constants 232 (>>00000) 7.6.2 Use of functions 234 (>>00000) 7.6.3 Code layout 235 (>>00000) 7.6.4 Commenting 237 (>>00000) 7.7 Recovering from errors 239 (>>00000) 7.8 Variables 242 (>>00000) 7.8.1 Variables and definitions 242 (>>00000) 7.8.2 Introducing names 247 (>>00000) 7.8.3 Predefined names 250 (>>00000) 7.8.4 Are we there yet? 250 Chapter 8 Technicalities: Functions, etc. 255 (>>00000) 8.1 Technicalities 256 (>>00000) 8.2 Declarations and definitions 257 (>>00000) 8.2.1 Kinds of declarations 261 (>>00000) 8.2.2 Variable and constant declarations 262 (>>00000) 8.2.3 Default initialization 263 (>>00000) 8.3 Header files 264 (>>00000) 8.4 Scope 266 (>>00000) 8.5 Function call and return 272 (>>00000) 8.5.1 Declaring arguments and return type 272 (>>00000) 8.5.2 Returning a value 274 (>>00000) 8.5.3 Pass-by-value 275 (>>00000) 8.5.4 Pass-by-const-reference 276 (>>00000) 8.5.5 Pass-by-reference 279 (>>00000) 8.5.6 Pass-by-value vs. pass-by-reference 281 (>>00000) 8.5.7 Argument checking and conversion 284 (>>00000) 8.5.8 Function call implementation 285 (>>00000) 8.5.9 constexpr functions 290 (>>00000) 8.6 Order of evaluation 291 (>>00000) 8.6.1 Expression evaluation 292 (>>00000) 8.6.2 Global initialization 293 (>>00000) 8.7 Namespaces 294 (>>00000) 8.7.1 using declarations and using directives 296 Chapter 9 Technicalities: Classes, etc. 303 (>>00000) 9.1 User-defined types 304 (>>00000) 9.2 Classes and members 305 (>>00000) 9.3 Interface and implementation 306 (>>00000) 9.4 Evolving a class 308 (>>00000) 9.4.1 struct and functions 308 (>>00000) 9.4.2 Member functions and constructors 310 (>>00000) 9.4.3 Keep details private 312 (>>00000) 9.4.4 Defining member functions 314 (>>00000) 9.4.5 Referring to the current object 317 (>>00000) 9.4.6 Reporting errors 317 (>>00000) 9.5 Enumerations 318 (>>00000) 9.5.1 “Plain” enumerations 320 (>>00000) 9.6 Operator overloading 321 (>>00000) 9.7 Class interfaces 323 (>>00000) 9.7.1 Argument types 324 (>>00000) 9.7.2 Copying 326 (>>00000) 9.7.3 Default constructors 327 (>>00000) 9.7.4 const member fun
Edited last time by Chobitsu on 02/01/2023 (Wed) 15:54:03.
>spare reserve
>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

Report/Delete/Moderation Forms
Delete
Report