C++ Lab: Potion Mixing

Sun Nov 02 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction


Task

The files referenced in this task can be downloaded here.

Create a small program which reads in an inventory file to two parallel arrays, then reads from a second file pairs of potion types and quantities of potions to mix and mixes potions using the resources in the read in inventory.

The inventory is stored in a .csv file similar to:

name,quantity potion base,100 limpwurt root,100 eye of newt,10 white berries,100

The first value in each record being the name of the item in the inventory and the second item being the quantity of that item in the inventory. Each item needs to be read in, split on the comma, and stored to parallel arrays. Once stored to parallel arrays then a second file containing pairs of potions to make with the quantities to make them in can be read, these pairs similar to:

strength 5 attack 10 defense 2

The first value in each record tells what type of potion to make, and the second how many potions of that type to make. To make a potion 1 potion base is always consumed along with a mixin depending on the potion to be made:

PotionMixinCost/Potion
attackeye of newt2
strengthlimpwurt root1
defensewhite berries10

So for 1 attack potion to be made 1 potion base and 2 eyes of newt must be consumed from the inventory. Or for 5 then 5 potion bases and 10 eyes of newt consumed. So for each potion to be made 1 potion base and:

amount=quantityWantedcostamount = quantityWanted * cost

Mixins must be used, where quantityWanted is the amount of potions wanted and cost is the cost of mixins needed per potion mixed.

To do this 4 functions must be written:

  1. void openFile(ifstream &file, string prompt) - Prompts for (using prompt) and reads in a file name, then opens the file name entered into file. If the file name entered doesn’t open the function loops reading a new file name until the file does open. Once a file opens into file the function stops executing.

  2. void readInventory(string names[], int quantities[], int &readCnt) - Calls openFile() passing it a variable created of ifstream type and the prompt "Inventory File: ". Then each record is read from the file, splitting the 2 pieces of data in the record on the comma, placing the data before the comma (the name of the inventory item) into names and the data after the comma (the quantity of the item in the inventory) into quantities, and finally incrementing readCnt.

  3. void displayInventory(string names[], int quantities[], int readCnt) - Iterates through the parallel arrays, outputting each record in the form " r) NAME: QUANTITY\n" where r is the item number (starting at 1) of the item in the inventory, NAME is the inventory item name, and QUANTITY is the amount of items of that NAME in the inventory.

  4. void createPotions(string names[], int quantities[], int &inventorySize) - Which does the following:

    a. Calls openFile() passing it a variable created of ifstream type and the prompt "Potions File: ".

    b. Finds the index of "potion base" in the inventory (linear search) and stores it to a variable.

    c. Repeatedly reads a potion type (guaranteed to be attack, strength, or defense) and qty (requested number of potions) from the potions file, and for each record:

    • Outputs "QUANTITY TYPE potions requested\n".

    • Determines the required mix-in item based on the type and saves its index (linear search) in the inventory to a variable. The cost of the mixin per potion made should also be saved to a variable. The mixins and their costs per potion are located in the table above.

    • Checks if there is enough potion base and mixin to create at least 1 potion of the requested type.

      • If there is no potion base in the inventory or the potion base quantity in the inventory is 0 output the error "Error: Not enough potion base to make TYPE potions\n" where TYPE is the type of potion requested to be made. Then continue to the next potion request from the file.

      • If the required mixin is not in the inventory or there is not enough mixin in the inventory to make at least 1 of the requested potions output the error "Error: Not enough NAME to make TYPE potions\n" where NAME is the name of the mixin needed and TYPE is the type of potion to make. Then continue to the next potion request from the file.

    • Checks if there are sufficient resources to make qty potions.

      • If there is not enough potion base to make the original qty of potions requested, limit the qty of potions to be made to how many potion bases there are in the inventory. Then output "Limited by LIMIT base potions\n" where LIMIT is the amount of potions able to be made based on the amount of potion base available.

      • If there is not enough mixer to make the original qty of potions requested, limit the qty of potions to be made to how many potions can be made with the resources in the inventory using:

      limit=amountOfMixinInInventory/costlimit = amountOfMixinInInventory / cost
      Where amountOfMixinInInventory is the quantity of the mixin in the inventory and cost is the cost (number of mixins) to make 1 potion requested. Then output "Limited by AMOUNT MIXIN\n" where AMOUNT is the quantity of the mixin in the inventory and MIXIN is the name of the mixin required.
      • The actual amount of potions to be made will be the smallest limit between the limits of qty from potion base and mixin.
    • Consumes the resources to make qty (possibly limited in the last step) potions by subtracting the qty potions to be made potion base and qty * cost mixins to make the potions from the inventory. Then outputs "Mixing QTY TYPE potions\n" where QTY is the number of potions made and TYPE is the type of potion made.

    • Adds qty (possibly limited) potions to the inventory by either:

      • Adding the type of the potion to the inventory with a quantity of the number of potions made if the type of potion created is not already in the inventory.

      • Adding the number of potions made to the quantity of the potions in the inventory if the type of potion created is already in the inventory.

    • Calls displayInventory() to show the updated inventory.

    d. Repeats step (c) for every type qty pair in the potions file.


Steps to Complete the Lab

All input files needed for this lab can be downloaded here.

You will need to:

  1. Write the functions described above.

  2. In main make 2 parallel arrays, one to hold names of items in the inventory as strings and another to hold quantities of items as integers. The max size of each array should be 10. A variable should also be created to hold the amount of items in the inventory.

  3. In main, call readInventory() passing it the parallel arrays and the variable to hold the amount of items read into the inventory.

  4. In main, call displayInventory() passing it the parallel arrays and the variable holding the amount of items read into the inventory.

  5. In main, call createPotions() passing it the parallel arrays and the variable holding the amount of items read into the inventory.


Libraries Permitted

You may only use the following libraries in your program:

  1. iostream

  2. fstream


Criteria for Success

See CodeGrade

Example Program Interaction

An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.

Inventory File: inventory.csv Inventory: 1) potion base: 100 2) limpwurt root: 100 3) eye of newt: 10 4) white berries: 100 Potions File: potions.txt 5 strength potions requested Mixing 5 strength potions Inventory: 1) potion base: 95 2) limpwurt root: 95 3) eye of newt: 10 4) white berries: 100 5) strength: 5 10 attack potions requested Limited by 10 eye of newt Mixing 5 attack potions Inventory: 1) potion base: 90 2) limpwurt root: 95 3) eye of newt: 0 4) white berries: 100 5) strength: 5 6) attack: 5 2 defense potions requested Mixing 2 defense potions Inventory: 1) potion base: 88 2) limpwurt root: 95 3) eye of newt: 0 4) white berries: 80 5) strength: 5 6) attack: 5 7) defense: 2

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.