CSCI 103 - Spring 2024 Introduction to Programming

CS102 Fall 2020 Final - Cart

Complete the following program for a simple online shopping application where users can add and remove products from their cart and then compute the total cost of the cart by filling in the missing parts of the code.

The program will start by asking the user how many products (i.e. numProd) they wish to enter and then read in numProd prices (doubles) into the array: prod_prices. Then the user will use commands: a to add an item or r to remove an item from the cart followed by the product ID (i.e. the index of the product’s price in the prod_prices array) until they enter q to quit. For example, if the user enters a 2 then the program should add product 2 (which costs 0.75) to the cart array.

When the user types in the command q, you should quit the program and output the total cost of the items currently in the cart. You may assume product IDs entered by the user exist (i.e. are between 0 to numProd-1). If the user tries to remove a product item, remove only the 1st occurrence of that item (not all occurrences). Also, if the item to remove doesn’t exist, the program should output an error message. Below is a sample execution of the program.

The program below has some missing elements. Complete the blanks (add code) to make the program function as desired. You may only fill in the blanks shown and cannot add or alter other code. A sample execution is shown below.

cart-example.png

The skeleton file can be downloaded with the link: cart.cpp

(You may need to right-click and choose Save As or Save Target As to download the .cpp file or open it in your browser and save the file.).