Pages

Saturday, 29 May 2021

Chapter 20 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 5

Define an input and output operator (<< and >>) for vector.


At first I was confused as to whether he meant std::vector or the vector we created. I decided to go with std::vector. We basically did this in Drill 14 of Chapter 19 by reading in { val, val, val } into vectors.

That seems like an odd way to type data in. So I decided on an input loop that just asks for data until you enter ctrl+z to break the loop. The main problem with this is that I have no idea how to allow reading a string with whitespace but also reading in other types. It kind of seems impossible but I'm not experienced enough to say for sure. At this point, I just ignored whitespace to bypass this problem but input and output operators for any type is begging for trouble.

EDIT 01/06/2021
After some thinking (and some googling) I realised that there is no graceful way to make a stringstream convert to any type. I was thinking of checking if T was of type std::string but comments rightfully said you should be specialising your functions more and knowing the kind of types it will be using. I eventually used this suggestion and created 2 input overloads; one for integers/characters and another for strings.

I also spent the evening adding google test to my project so I could add some automated unit tests to my project and not have to faff about with input on a console window when testing. You can read about it here:

Friday, 28 May 2021

Chapter 20 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 4

Find and fix the errors in the Jack-and-Jill example from section 20.3.1 by using STL techniques throughout.


I didn't see anything completely serious. High() can return a pointer to garbage if the containers passed in are empty. To combat this getFromJack() and getFromJill() check to see if the file is empty. If it, it returns a nullptr. That way we're not doing any unnecessary memory allocations and we can gracefully handle checks for nullptrs without crashing the program with errors. Having a nullptr is perfectly fine so I didn't want to error if the file was empty. 

I also modified getFromJill() to be able to handle any size to prevent unwanted allocations. getFromJack() is a bit more difficult as array size must be specified at compile time. A way to get round that is to read jack's data into a vector first then create the array from that size but that kind of defeats the point of using an array. I'm going to leave it the way it is for getFromJack() as arrays should be used when you know the size.

EDIT 14/06/2021
I showed this exercise to one of our senior engine programmers who pointed out that there is no handling for if the pointers supplied to high() are the correct way round. He said this could cause a near infinite loop. To be honest it never occurred to me to pass in the pointers the wrong way round, why would someone do that? I guess that's why I'm not a QA tester.

Tuesday, 25 May 2021

Chapter 20 // Exercise 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 3

Look at the palindrome examples (section 18.7); redo the Jack-and-Jill example from section 20.1.2 using that variety of techniques.


The wording of this confused me as those palindrome examples are about palindromes; i.e., words that are the same backwards and forwards; what has that got to do with reading in doubles?

But I stared at section 18.7.1 and realised at the heart it's just comparing two things which is what high is doing. So I re-did high to compare using the techniques from the string, array and pointer examples.

The "string" one was a bit tricky to adapt as there's really no point in going backwards unless I'm comparing two, then comparing again against the current high.

The array one was simple enough and I could use indexing which is always nice. Also, you don't need to change Jill's data into an array as a vector is basically an array; you just give it the address of the first element and the size.

I didn't do the pointer one because I had already done that in exercise 2.