/robowaifu/ - DIY Robot Wives

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

I Fucked Up

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)


“If you are going through hell, keep going.” -t. Winston Churchill


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.
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