Question: C++ This week we are continuing our exploration of structs and arrays. Last time we created a struct that contain an array. This time, we
C++
This week we are continuing our exploration of structs and arrays. Last time we created a struct that contain an array. This time, we are making a struct and then we are making an array of that struct.
Details
For this lab, we are going to be managing a list of widgets. Our widgets will be the struct we are creating. The widget will have 3 fields
name
quantity
cost
Then we will make an array of this struct of size 10. We have to wait another week to learn how to grow our arrays.
Input
For our input, we will have a command file. The command file will have two commands
load - this will be followed by a filename. The file name will indicate the name of the data file to be used during the loading. The format of the data will be described below.
showMeTheWidget - this will be followed by a number. The number indicates the index of the widget to be shown. Only show the widget if the location is valid, i.e. at least 0 and less then the number of widgets in the array.
Data
The data will be a single word followed by an integer followed by a double. There will be at least 1 space between each of the items on the line. You can use extraction to read each of the data items. The string is the name of the widget, the integer is the quantity of the widgets, and the double is the cost of the widget.
Soap 10 3.85 Glasses 54 109.5 Dogs 1 145.65 Cats 1000000 .25 Fish 100000000 .01
That is a sample of what the data file could look like.
load widgets.txt showMeTheWidget 1 showMeTheWidget 15
That is a sample of what a command file may look like. The output will be shown below.
Output
Each command will be echoed along with the arguments for the commands. Then any results will be displayed as a result of the command.
Command: load widgets.txt Loaded: 5 Command: showMeTheWidget 1 Name: Glasses Quantity: 54 Cost: $109.5 Command: showMeTheWidget 15 Sorry, bad widget number 15
load for the load command display the name of the file then on the next line say Loaded: and the number of widgets in the array.
showMeTheWidget for the showMeTheWidget command show the requested widget number and if the number is valid on the next three lines show the name, quantity, and cost of the widget. Use the words Name:, Quantity: and Cost: before each field and you must also add the $ to the cost. For costs that would normally end in a 0 dont do anything. We will ignore this problem for now. If the widget isnt found, i.e. a bad widget number is given then indicate that as shown above.
Requirements
Make a struct for the widget with the 3 fields as indicated above.
Declare the function void widgetManager( string input, string output ); in a file named lab11.h.
The array should be of size 10.
There is not a requirement to write functions, but a nice function to write would be 1 to read in a widget and one to display a widget.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
