DEV Community

Cover image for Late Hacktoberfest Part lll
Amir Helali
Amir Helali

Posted on

Late Hacktoberfest Part lll

Third Contribution

C++ was the first coding language I learned and it has been some time since the last time I used it for any of my projects. So, for my third contribution I decided to contribute to a C++ project just to see if I am still capable of utilizing the language and the tools it offers. I searched for a C++ issue on Github and decided to work on this issue. The project owner wanted to create a functionality for his expenditure tracking console application that went though a spendings csv file for a day and calculated the total amount spent for that day. So I reached out to the owner to assign me the issue and began to work after I was assigned to it.

Implementation

I forked the the repository, created a new branch for the issue and started to work on it. For the implementation I decided to create 3 different functions:

  1. A function that was in charge of reading a CSV file and retrieving the data in it as a vector type. This function took a reference to a file path as a parameter and returned a vector of data from the file. The screenshot of the function can be found below:
    Image description

  2. A function that was in charge of going through the data returned from the previous function and took a reference to that data and a column name reference as parameters. This function went through the vector of data and looked for the index of a specific column based on its name. After finding it's index it went through all the values associated with that column based on the index found, saved all the values in a vector and returned them. This screenshot shows this function:
    Image description

  3. A function that took a reference of all the values returned from the previous function and calculated the sum of all of them. Since I have used vectors to store all my data, I decided to use the Algorithm library and utilize transform() and accumulate() functions from it. I used transform and lambda expression to change the type of the values from string to double and used accumulate to get the sum of all the double values. This screenshot shows the implementation:
    Image description

After I implemented all the functions and tested them out to make sure everything worked. I committed my changes and created a PR.

What I Learned

I learned that I can still code in C++ even though it has been a few years since the last time I used it. I had to go over the documentation for the algorithm library again to review how transform() and accumulate() functions worked. So, overall this was a nice review on a language I haven't used in a long time and it rekindled some old knowledge for me.

Top comments (0)