Oh C++, How I've Missed You So!
Blog Home

Oh C++, How I've Missed You So!

July 19, 2019 |  Categories:  Development  

Oh how I wish that statement were true. Don't get me wrong, C++ is an enchanting language in many ways. But after having spent such a long time away from it, I've slow to readjust. Working on this new project has really underscored to me how accustomed I had become to Python's easy-going and flexible nature as an interpreted language. Thus far, I think my biggest struggle in C++ has been to write in a consistently object oriented manner. Although the fragmented nature of my project has made this difficult from the beginning, I've come dangerously close to producing spaghetti code on a number of occasions. While I've gotten 9 out of my 10 functionalities up and running properly, my body of code is an absolute train wreck. At this point, it's bordering 1,000 lines, with each functionality consisting of several child functions tied together by a web of interdependencies. Once I finish up my last function (the binary/decimal calculator) tomorrow, my first objective will be to break up my source code into more digestible and easily portable modules. I don't know exactly how to do this yet, but that will be one of the key lessons from this project. Much like you can import functions from separate modules in Python, I'm sure you can do so in C++, and making this modification to my project will significantly boost the readability of my code. Here's a sneak preview of my app's main menu in the terminal! ![Screenshot of Puzzle App's main menu][1] Very exciting, I know. Who doesn't love that iconic black screen of the command line? After cleaning up my source code, I then plan to watch and follow along with a few tutorials on C++ GUI design using the Qt framework. With the core, backend functionality of my program complete, I'm really hoping that the GUI design will just be a matter of integrating the widgets with the input and output portions of my code. Otherwise, I could be in for a world of hurt! At any rate, I spend the majority of my coding today focused on implementing a change calculator much like I did in Python. As I mentioned earlier though, the typed nature of C++ made this quite a bit trickier. In Python, I never had to account for the complexities that can arise from performing mathematical operations on variables of different types (eg integer vs float). Essentially, the primary algorithm underlying a change calculator is this: 1. Initialize two lists/arrays: one to hold the value of each change denomination ($10, $0.25, etc.) and another to hold the required number of each denomination 2. Take the difference between the price and payment (eg $25.34) 3. Calculate the integer quotient between the difference and the highest remaining denomination value ($25.34 / $20 = 1) 4. Recursively subtract the product of the integer in 3 and its denomination value from the difference 5. Repeat this process until you reach the number of pennies Confused? Hopefully not. To illustrate this process, let's take a look at how I implemented this in both C++ and Python. First, my Python code: ![Screenshot of Python change calculator][2] As you can see, I started by creating my denomination value lists, calculated the difference between the price and payment, and implemented the loop I described in steps 3-5. It was this exercise specifically that made me so grateful for the list data structure in Python. It makes life so much easier! And now for the C++ code: ![Screenshot of C++ change calculator][3] As you can see, my code looks a little different. The first float declaration was simply a placeholder so I could pass the original change difference value (payment-price) to my print function. Also, you'll notice that I had to calculate the difference in a different function. This again was due to the typed nature of C++, as including that process here would have led to a grossly bloated function. In essence though, the process is the same in both cases. And, just as with my Python script, this code produces the correct answer when given test values! ![Screenshot of change calculator in terminal][4] Hopefully that explanation made sense. I've always been a much better at teaching through speech as compared to writing, but I'll keep sharing these code-breakdowns from time to time to hopefully improve. The heat that's been sitting over Pocatello is finally supposed to break tonight, so I'm planning on a nice hike tomorrow morning to enjoy the cool air while it lasts. I'll plan on writing another update on Sunday to fill you in on my progress with the GUI design. Until then, I hope everyone has a nice start to their weekends! -Joe [1]: https://pickert-website-static.s3.us-east-2.amazonaws.com/blog/19-7-19/Screen+Shot+2019-07-19+at+2.18.26+PM.png [2]: https://pickert-website-static.s3.us-east-2.amazonaws.com/blog/19-7-19/Screen+Shot+2019-07-19+at+2.42.45+PM.png [3]: https://pickert-website-static.s3.us-east-2.amazonaws.com/blog/19-7-19/Screen+Shot+2019-07-19+at+2.33.19+PM.png [4]: https://pickert-website-static.s3.us-east-2.amazonaws.com/blog/19-7-19/Screen+Shot+2019-07-19+at+2.18.46+PM.png

Leave a comment:



I'm Joe Pickert, and welcome to my blog.

Here, you can find a collection of my thoughts both tech-related and otherwise.

If you find something that interests you, please leave a comment! I'd love to hear your thoughts and feedback!