Back to projects

// C++ Fundamentals Project

Bakery CostCalculator

A C++ console program that reads ingredient and product data from files, calculates production costs, and prints the results in a formatted table.

C++ File I/O Arrays Functions Console Output
product cost =
sum(
  ingredient amount
  * ingredient price
)

highest cost:
Cherry Wafer

// The Project

Applying C++ fundamentals to structured data

I built this calculator for my CSCI 2170 course. The assignment gave me a practical way to combine concepts I was learning in C++: opening files, storing related values in arrays, breaking a program into functions, performing calculations with nested loops, and formatting results for the console.

The program reads product names from one file and a matrix of ingredient amounts and prices from another. It calculates each product's cost, displays all seven products in a table, and identifies the most expensive item.

// Interactive Walkthrough

From input files to a calculated cost table

The walkthrough uses the same seven products, six ingredients, and calculated prices shown in the project's sample output.

Input data 7 products x 6 ingredients
Ingredient amounts 3 of 7 products shown
Product Ing 1 Ing 2 Ing 3 Ing 4 Ing 5 Ing 6
Donut 10 20 50 25 0 0
King Cake 5 50 40 90 0 1
Cherry Wafer 12 27 25 10 0 15
Unit prices $5.14 $10.25 $8.50 $1.50 $20.00 $72.25
Console Ready
$ ./bakery-cost-calculator
Enter the ingredient data file: [loaded]
Enter the product names file: [loaded]

Press "Run Calculation" to continue.

// How It Works

A file-to-report workflow

  1. 01

    Open

    Ask for both filenames and keep prompting until each input file opens successfully.

  2. 02

    Read

    Load product names, ingredient amounts, and ingredient prices into fixed-size arrays.

  3. 03

    Calculate

    Use nested loops to multiply each ingredient amount by its unit price and total each product.

  4. 04

    Compare

    Scan the calculated prices and keep the index of the highest-cost product.

  5. 05

    Print

    Use stream formatting to align the ingredient values and display prices to two decimal places.

Challenge

Keeping related file data aligned

The product names, ingredient quantities, and ingredient prices come from separate parts of the input. The main challenge was storing them so the same product index stayed aligned throughout the calculation and output. Breaking the work into separate functions made the data flow easier to reason about.

What I Practiced

  • Opening, validating, reading, and closing input file streams.
  • Using one-dimensional and two-dimensional arrays.
  • Passing arrays and values between focused functions.
  • Using nested loops for calculations and formatted table output.

Current Limitations

  • Uses fixed maximum sizes of 15 products and 20 ingredients.
  • Requires input files to follow a specific numeric format.
  • Displays results only in the console.
  • Calculates production cost but does not include selling price or profit margin.

Possible Improvements

  • Replace fixed arrays with vectors and structured product types.
  • Add clearer validation for incomplete or malformed data.
  • Export the completed cost table to a file.
  • Add selling-price and profit-margin calculations.

// Source Code

Review the C++ implementation.

The repository contains the source file, sample datasets, and the complete console output.