Pages

Saturday, 29 February 2020

Chapter 11 // Exercise 6 - 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 11 // Exercise 6

 Write a program that replaces punctuation with whitespace. Consider .(dot),
 ; (semicolon), ,(comma), ? (question mark), - (dash), ' (single quote)
 punctuation characters. Don't modify characters within a pair of double
 quotes ("). For example "-don't use the as-if rule." becomes " don t use the
 as if rule ".
I'm sure there's a more elegant way to do this than if's and counting quotes but it isn't too ugly to I'll allow it.

Friday, 28 February 2020

Chapter 11 // Exercise 5 - 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 11 // Exercise 5

 Write a program that reads strings and for each string outputs the character
 classification of each character, as defined by the character classification
functions presented in section 11.6. Note that a character can have several
 classifications (e.g, x is both a letter and an alphanumeric).


This was suspiciously easy and it makes me wonder if I did it correctly.

Thursday, 27 February 2020

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

Write a program called multi_input.cpp that prompts the user to enter several
integers in any combination of octal, decimal, or hexadecimal, using the 0
and 0x base suffixes; interpret the numbers correctly; and converts them to
decimal form. Then your program should output the values in properly spaced
columns like this:
0x43 hexadecimal converts to 67 decimal
0123 octal converts to 83 decimal
  65         decimal converts to 65 decimal


The useful item I learnt from this exercise is that stoi() takes in 3 parameters, the first is the string, the second is a size_t pointer (used for finding the next value) and last is a number which indicates the base. I noticed that when you use stoi() on an int, the base is default set to 10 (deicmal), therefore it cut off the 0 and 0x, even if the cout stream was set to oct and hex. To convert the number correctly, set the base to 0 as this will check for all 3 bases.

If you just want to check for oct, set the base to 8 and hex to 16. Other than that, the hardest part was getting it to output with all the fields lined up. I really hate that setw().

Wednesday, 26 February 2020

Chapter 11 // Exercise 3 - 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 11 // Exercise 3

Write a program that removes all vowels from a file ("disemvowels"). For example, Once upon a time! becomes nc pn tm!. Surprisingly often, the result is still readable; try it on your friends. 


Once again the find functions on a string come to the rescue. I think I'm becoming a bit addicted to using find_first_not_of and find_first_of; they're just so god damn handy. I know these functions haven't been introduced in the book yet but this isn't prior knowledge I already had. I only learnt about them when googling how to solve problems like "find a character in a string" so I don't think it's cheating. Also rdbuf() and stringstreams are definitely ones I'll have to remember.

Tuesday, 25 February 2020

Chapter 11 // Exercise 2 - 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 11 // Exercise 2

Write a program that given a file name and a word outputs each line that contains that word together with the line number. Hint: getline().


.

Monday, 24 February 2020

Chapter 11 // Exercise 1 - 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 11 // Exercise 1

Write a program that reads a text file and converts its input to all lower case, producing a new file.


.

Sunday, 23 February 2020

Chapter 11 // Drill 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 11 // Drill 10

Make a small table including last name, first name, telephone number and email address for yourself and at least five of your friends. Experiment with different field width until you are satisfied that the table is presented well.


This drill seemed a little pointless to me seeing as how I don't see how setw() can be useful outside of the console window. And unfortunately DOS isn't all that popular anymore. You can still use it with user defined streams like ostream but whatever. Handy to know when I start making all my console window games...

Saturday, 22 February 2020

Chapter 11 // Drill 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 11 // Drill 9

Write some code to print the number 1234567.89 three times, first using defaultfloat, then fixed, then scientific forms. Which output form presents the user with the most accurate representation? Explain why.


This exercise made me wonder why anyone would use float if they need precision from floating point numbers. Sure float is smaller than double so you would be saving bytes however you lose precision.

Friday, 21 February 2020

Chapter 11 // Drills 1,2,3,4,5,6,7,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 11 // Drills 1,2,3,4,5,6,7,8
These drills were very easy...a little to easy. It made me suspicious. 

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.