Thursday, 20 February 2020

Chapter 10 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 10 // Exercise 11

 Write a program that produces the sum of all the whitespace separated integers in a text file. For example, bears: 17 elephants 9 end should output 26.


I was going to leave this last exercise for the night but seeing as how it was the last one of the chapter I decided to just get it done and ended up doing it in 5 minutes. This was very easy if you just read everything into a vector, sort it and then convert the string to an int using stoi(). If it's not an int then we've run out of numbers and the program breaks out of the for loop and prints the sum.

It's funny, I remember almost 3 years ago now when I was starting the second semester of my first year at uni and in the programming exam we had coming up, we had to read in from text files and output to them. I froze and started freaking out because I had only done up to around chapter 5 by then. I thought the task was impossible and I'd never be able to do it; that I wasn't good enough or smart enough to be able to remember all these coding conventions. Progress takes time and patience; not all of us are born as geniuses. Just keep working at it and don't give up.

And with that I have FINALLY finished Chapter 10. I originally started this chapter in June 2017 and it's crazy to think I'm just now moving onto a fresh chapter. I remember giving this a read through a few years ago but it's all new content from here on out. I'm excited to start the graphics chapters though and he starts to introduce templates and inheritance. I hate these constructs but I work with Unreal and UE4 loves object oriented programming...and I mean, it really fucking loves it. Personally, I detest OOP but that's what the job requires so I need to understand it better so I can undo it and replace it with faster data orientated code.

Wednesday, 19 February 2020

Chapter 10 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 10 // Exercise 10

         Add a command from x to the calculator from Chapter 7 that makes it take input from a file x. Add a command to y to the calculator that makes it write its output (both standard output and error output) to file y. Write a collection of test cases based on ideas from section 7.3 and use that to test the calculator. Discuss how you would use these commands for testing.


I cheated slightly on this one and modified the calculator to only take input from a file and then output to a file; if there is an error it will ask you to press ';' to continue. This was pretty simple as you can continue to use cin.get() and cin.unget() on ifstreams. If you run the txt file I provided, you'll notice that it just keeps going forever because it gets stuck in a loop on 'a';. This isn't broken, it expects you to input a correct definition however, 'a' keeps getting putback and then being re-read. If I wasn't feeling lazy I would stick a command in that would switch to regular calculator code and allow you to start inputting your own stuff.

Tuesday, 18 February 2020

Chapter 10 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 10 // Exercise 8

Write a program that takes two files containing sorted whitespace separated words
and merges them, preserving order.


At first I completely missed the word "sorted" and thought this was the exact same exercise as the on before. After that I changed the program slightly to output every word into a vector and then sorted it; outputting it back into another file. Easy peasy.

Monday, 17 February 2020

Chapter 10 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 10 // Exercise 8

Write a program that accepts two file names and produces a new file that is the contents of the first file followed by the contents of the second; that is, the program concatenates the two files.


Ah, a nice and easy one. I also really love using substr(), it has so many useful applications, like checking to see if a certain part of a string matches another string. I also found that you can easily copy files into another by using the .rdbuf() function. It contains a pointer to the contents of the file for easy access.

Saturday, 15 February 2020

Chapter 10 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 10 // Exercise 7

Make a version of the calculator from Chapter 7 that accepts Roman Numerals rather than the usual Arabic ones, for example, XXI + CIV == CXXV.


When I first tried this I realised we needed a way to convert Roman Numeral to Ints so went back to the previous exercise and added a new function that does that. Then it became as simple as adding the class to the file and checking input for roman numerals. Once they are all added, they are then converted to an int, the calculations are done and then the resulting int is converted back to a Roman Numeral.