Wednesday, 25 May 2022

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

Modify the table-checking program from section 23.9 to write a new table where the rows with the same initial digit (indication the year: first grades start with 1) are merged.


I honestly was really puzzled by this one. I guess he meant do exactly the same but while checking the lines create a new file where years are merged? So instead of seperate lines for 1A and 1B they get merged into 1? I hope that's what he meant because that's what I did.

I tried to keep things relatively simple. I originally thought of pushing back the table into a vector then erasing lines to create the table but then remembered this chapter was also about maps, not just regex. Maps can store a unique key which was perfect for checking to see if the current line had already been pushed back with the class sizes.

Tuesday, 24 May 2022

Chapter 23 // Exercise 9 - 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 23 // Exercise 9

Using eof(), it is possible to determine which line of a table is the last. Use that to (try to) simplify the table-checking program from section 23.9. Be sure to test your program with files that end with empty lines after the table and with files that don't end with a newline at all.


from_string() also has to be created for the code to work. This is found on page 853 but I added it to my running version of std_lib_facilities.

This was an annoying one to read as it's hard to determine how many tab/spaces there are in the printed book. I also had to type up the table as he doesn't provide it and made sure it matched the patterns he gave.

I also misunderstood the question and thought that he was suggesting that you could use eof() to determine newlines?? My brain goes to strange places. Anyway, I added a simple check that looks for new lines anywhere to skip them.




Monday, 23 May 2022

Chapter 23 // Exercise 8 - 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 23 // Exercise 8

Modify the program from section 23.8.7 so that it takes as inputs a pattern and a file name. Its output should be the numbered lines (line-number: line) that contain a match of the pattern. If no matches are found, no output should be produced.


This didn't require much changing from the code given; just adding in the lines to get a filename and open it.

Sunday, 22 May 2022

Chapter 23 // Exercise 7 - 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 23 // Exercise 7

Write a program (similar to the one in the previous exercise) that finds credit card numbers in a file. Do a bit of research to find out what credit card formats are really used.


So I thought credit card numbers were straight forward; literally just 16 digits separated by a space (at least that's what it is on my credit card). I never realised they were all different, I've only ever had a mastercard.

I found this site:
and decided to write patterns for the 3 most common; visa, mastercard and amex. This site was also incredibly useful:

I got a load of test credit card numbers from this site and added some more to it:

Whilst doing this I saw more posts about how regex should not be used for data validation. I even found sites like paypal providing test credit card numbers and they had all spaces stripped out.

I did check for any whitespace separating characters after the max length of the number though because, well they should be separated in a text file anyway.

Saturday, 21 May 2022

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

Write a program that finds dates in a text file. Write out each line containing at least one date in the format line-number: line. Start with a regular expression for a simple format, e.g., 12/24/2000, and test the program with that. Then, add more formats.


First I copied the text from this page: 
https://help.scribesoft.com/scribe/en/sol/general/datetime.htm into a text file for testing and used https://regex101.com/ to help me create the right patterns.

After that I ended up with 5 different patterns to test various date formats including ISO. I didn't add validation to the patterns as I feel validation should be done once you have the strings, regex is just for searching.