This repository contains the source code and supporting files for the Grocery Tracker program, developed as part of CS 210: Programming Languages. The program is a C++ console application designed for the Corner Grocer to analyze daily purchase records of produce items. It reads from an input file (CS210_Project_Three_Input_File.txt) listing items in chronological order, tracks their frequencies using a map-based data structure, and provides menu-driven options to query specific item frequencies, display a full frequency list, generate a text-based histogram, and exit the program. Additionally, it automatically generates a backup file (frequency.dat) with the accumulated frequency data for persistence.
The core problem it solves is helping the store optimize their produce section layout by identifying purchase frequencies, enabling data-driven decisions on item placement for better customer flow and sales.
As mentioned in the overview, this project builds an item-tracking tool that processes text-based sales logs to compute and visualize item purchase frequencies. The Corner Grocer generates these logs daily, but manually analyzing them for patterns (e.g., how often "Cranberries" are bought) is inefficient. The program automates this with secure file I/O, efficient data storage via std::map, and user-friendly outputs, turning raw data into actionable insights like histograms for quick visual reference.
I excelled in implementing a clean class-based design with the ItemTracker class, which encapsulated the frequency map in a private section while exposing intuitive public methods—this promoted good object-oriented principles right from the start. The use of std::map for O(log n) lookups was spot-on for the scale, and I handled file operations robustly with error checks. The menu loop with basic input validation (e.g., ensuring numeric choices) made the program user-proof without overcomplicating it.
Where could you enhance your code? How would these improvements make your code more efficient, secure, and so on?
One area for enhancement is adding case-insensitive item matching (e.g., treating "apples" and "Apples" as the same) using std::transform to lowercase strings before insertion—this would improve usability without much overhead. For security, I could validate file paths to prevent directory traversal attacks, though it's low-risk here; this would make it more robust for real-world deployment. Efficiency-wise, switching to std::unordered_map could reduce lookup time to O(1) for larger datasets, trading some memory for speed in high-volume scenarios.
Which pieces of the code did you find most challenging to write, and how did you overcome this? What tools or resources are you adding to your support network?
The trickiest part was handling input buffering issues after reading integers for menu choices, which caused getline to skip user-entered item names. I overcame this by using cin.ignore(10000, '\n') to clear the newline character, tested iteratively in Visual Studio's debugger. For future reference, I'm adding zyBooks sections on stream manipulators (e.g., 7.4) and the C++ Reference site for std::istream behaviors to my bookmarks, plus Stack Overflow for quick stream troubleshooting—it's been a reliable quick-win resource.
Key transferable skills include file I/O with fstream for reading/writing structured data, which applies to any data-processing app (e.g., CSV parsers in future Python courses). Map usage for key-value storage is versatile for caching or databases in web dev. The menu-driven interface with validation translates directly to CLI tools or even GUI event handlers. Overall, my growing comfort with C++ classes and error handling will boost efficiency in multi-language projects, like integrating C++ backends with JavaScript frontends.
Maintainability comes from modular methods in the ItemTracker class (e.g., separate loadData, printHistogram), allowing easy swaps like changing the map type. Readability is enhanced by descriptive variable names (e.g., frequencies), inline comments explaining logic (e.g., "Increment the count for this item"), and consistent formatting with 4-space indents. For adaptability, the design uses const-correctness (e.g., const methods for queries) and avoids hardcoding (e.g., filename as a parameter), so extending to new features like JSON output or multi-file inputs would require minimal changes—just overriding or adding methods.
- GroceryTracker.cpp: Main source code with the
ItemTrackerclass and program logic. - CS210_Project_Three_Input_File.txt: Sample input data for testing (produce purchase log).
- frequency.dat: Generated output file (run the program to create/update this).
- Use Visual Studio (C++ Console App project).
- Place
CS210_Project_Three_Input_File.txtin the project directory. - Build (Ctrl+Shift+B) and run (F5).
- Interact via the menu;
frequency.datauto-generates on startup.
This project demonstrates proficiency in C++ for secure, efficient data analysis—feel free to clone and extend!