Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ This week we are continuing our exploration of structs and arrays. Last time

ID: 3701730 • Letter: C

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 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.

That is a sample of what the data file could look like.

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.

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 don’t do anything. We will ignore this problem for now. If the widget isn’t 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.

Explanation / Answer

CODE

lab11.h

struct widget {

            string name;

            int qty;

            double cost;

}s[10];

void widgetManager( string input, string output );

void readWidget(String);

void showMeTheWidget(int);

lab11.cpp

void readWidget(String f){

            // load the file and read values

            cout<<"Command: load "<<f<<endl;

           

            ifstream infile;

    infile.open(f);

    if(!infile){

        cout<<"Error reading file......"<<endl;

        exit(1);

    }

            // Read 10 widgets from file and load it into s[]

            for(int i=0; i<10; i++){

                        string inp;

                        cin>>inp;

                        istringstream iss(inp);

                        vector<string> results(istream_iterator<string>{iss}, istream_iterator<string>());

                       

                        s[i].name = results[0];

                        s[i].qty = (int)results[1];

                        s[i].cost = (double)results[2];

            }

            cout<<" Loaded: "<<10<<endl;

}

void showMeTheWidget(int index){

            if(index <0 || index >10){

                        cout<<" Sorry, bad widget number "<<index;

                        return;

            }

            cout<<"Command: showMeTheWidget 1"<<endl<<" Name: "<<s[index].name<<endl<<" Quantity: "<<s[index].qty<<endl<<" Cost: $"<<s[index

            ].cost<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote