• Home
  • Blog
  • Diablo Valley College CS Lab Maps and Stock Prices C++ Programming Task

Diablo Valley College CS Lab Maps and Stock Prices C++ Programming Task

0 comments

The STL (Standard Template Library) in C++ provides many common data structures used in day-to-day programming. The purpose of the STL is to provide some common code that many programs are expected to use, so software developers don’t have to “reinvent the wheel” every time they write a new program. In this lab we’ll explore the use of a map, which holds a (key, value) pair and provides fast retrieval of a value, given a key.

Assignment

Write a C++ program that provides a simple stock symbol lookup service based on an input data file. It should prompt the user for a menu to either (1) load a data file, or (2) get the stock price of a single stock.

Use an STL map to hold a (key, value) pair of a stock symbol (i.e. GOOG for Google) and its closing price from the previous day. The CSV file provided in this module should serve as the input file to the program. This CSV file has only two columns: a stock symbol and its most recent closing price (“simulated” data, not actual closing prices). To process the file, you may want to use your readSingleApp() function from the previous lab, modifying it to read a stock symbol and a closing price instead of application info.

Requirements

  1. Your program must use a map template to store a stock price symbol (a string) and a stock price (a double). An overview and some examples are provided in pp. 1054-1061 of the textbook (in Chapter 17).
  2. Output the stock price with two digits past the decimal point precision and a dollar sign ($).
  3. Each time a file is loaded, overwrite all the data in memory from the previous load.
  4. The user should be able to fetch any stock symbol’s price from the file.
  5. Loop for input until the user requests an exit (types a ‘3’).

Hints

Maps are templates and need two types specified, just like how a vector needs a single type. To make a map that has a string key and a double value, you would need this:

map<string, double> myMap;

You can then use array-like syntax to assign a value to a key. For example, to assign the value 1301.10 to the key “GOOG”:

string symbol = "GOOG";
myMap[symbol] = 1301.10;

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}