Friday, 8 October 2021

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

Define an Order class with (customer) name, address, data, and vector<Purchase> members. Purchase is a class with a (product) name, unit_price, and count members. Define a mechanism for reading and writing Orders to and from a file. Define a mechanism for printing Orders. Create a file of at least ten Orders, read it into a vector<Order>, sort it by name (customer), and write it back out to a file. Create another file of at least ten Orders of which about a third are the same as in the first file, read it into a list<Order>, sort it by address (of customer), and write it back out to a file. Merge the two files into a third using std::merge().

Github: 

Dear god, what a tricky exercise....or rather I made it tricky for myself. Instead of just using the date class we made way back in Chpater 9 or something I decided to use DateTime. Unfortunately, the new lovely std date class is only available in preview for C++20 right now so I had to use CTime. Figuring out how to parse the dates and convert them was a bit trickier than I thought but I got there in the end and it gives a nice official looking timestamp on the purchase using the pc's time.

Then std::merge() didn't work how I thought it would so that was something new.

No comments:

Post a Comment