Pages

Tuesday, 14 January 2020

Chapter 10 // Exercise 4 - 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 4

Modify the store_temps.cpp program from exercise 2 to include a temperature suffix c for Celsius or f for Fahrenheit temperatures. Then modify the temp_stats.cpp program to test each temperature, converting the Celsius reasdinfs to Fahrenheit before putting them in the vector.


It was in this exercise that I realised I had goofed the median bit and accidentally left in out of bounds errors (I also forgot to add a check for if the vector contained 1 value). I decided to add some more error checking as well as whilst re-doing the book I became quite lax and focused more on getting the exercises done than adding code that was actually robust.

I also prefer my new "isNumber()" function over the previous one that I used to use all the time. I think allowing cin to fail and then clearing it is a lot more of a hassle than just reading into a string. Cin will never go bad and you can just use the extremely handy "find_first_not_of()" function found in the std library. I love that function. This also allows for some easy parsing to extract numbers and characters.

Another thing I learnt whilst doing this exercise is that when using while(!file.eof) and ctrl+z, the last line will actually be read twice before the eof is read. This is not great if you are pushing things back into vectors. I kept getting an extra value and wondered where it was coming from. Instead, I'll remember to always start the while loop with while(cin >> value) as the eof will be read immediately. 

No comments:

Post a Comment