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

modify the bagTester function so that it reads a list of names from a file (read

ID: 3744623 • Letter: M

Question

modify the bagTester function so that it reads a list of names from a file (read until end of file) and add them to the bag.

Add them directly - do not put them into an array.

It should work on a file of any size. Modify the code so that the number of items is not hard coded (it is now 6)

read the additional value from the user (in the text it is hard-coded as "extra").

CODE: in c++

#include "BagInterface.h"

#include "ArrayBag.h"

#include "LinkedBag.h"

#include <iostream>

#include <string>

#include <fstream>

void displayBag(BagInterface<std::string>* bagPtr)

{

std::cout << "The bag contains " << bagPtr ->getCurrentSize()

<< " items:" << std::endl;

std::vector<std::string> bagItems = bagPtr->toVector();

int numberOfEntries = bagItems.size();

for (int i = 0; i < numberOfEntries; i++)

{

std::cout << bagItems[i] << " ";

} // end for

std::cout << std::endl << std::endl;

} // end displayBag

void bagTester(BagInterface<std::string>* bagPtr)

{

std::cout << "isEmpty: returns " << bagPtr->isEmpty()

<< "; should be 1 (true)" << std::endl;

std::string items[] = { "one", "two", "three", "four", "five", "one" };

std::cout << "Add 6 items to the bag: " << std::endl;

for (int i = 0; i < 6; i++)

{

bagPtr->add(items[i]);

} // end for

displayBag(bagPtr);

std::cout << "isEmpty: returns " << bagPtr->isEmpty()

<< "; should be 0 (false)" << std::endl;

std::cout << "getCurrentSize returns : " << bagPtr->getCurrentSize()

<< "; should be 6" << std::endl;

std::cout << "Try to add another entry: add("extra") returns "

<< bagPtr->add("extra") << std::endl;

} // end bagTester

int main()

{

char userChoice;

BagInterface<std::string>* bagPtr = nullptr;

std::cout << "Enter 'A' to test the array-based implementation "

<< " or 'L' to test the link-based implementation: ";

std::cin >> userChoice;

if (toupper(userChoice) == 'A')

{

bagPtr = new ArrayBag<std::string>();

std::cout << "Testing the Array-Based Bag:" << std::endl;

}

else

{

bagPtr = new LinkedBag<std::string>();

std::cout << "Testing the Link-Based Bag:" << std::endl;

} // end if

std::cout << "The initial bag is empty." << std::endl;

bagTester(bagPtr);

delete bagPtr;

bagPtr = nullptr;

std::cout << "All done!" << std::endl;

return 0;

} // end main

Explanation / Answer

Given below is the modified code which prompts for the input file name containing data. It adds all the strings from file into the bag.
Also prompts one entry from user and adds it. Since you have not provided all the necessary files, I could not test it. But I'm sure it will run for you.
You will need to create an input file with some strings and provide the file name when you run the code.


void bagTester(BagInterface<std::string>* bagPtr)
{

std::cout << "isEmpty: returns " << bagPtr->isEmpty() << "; should be 1 (true)" << std::endl;

std::string filename;
std::cout << "Enter input filename containing data to be added to bag: ";
std::cin >> filename;

std::ifstream infile(filename.c_str());

if(infile.fail()){
std::cout << "ERROR: could not open input file " << filename << std::endl;
}

std::string str;

while(infile >> str)
{
bagPtr->add(str);
} // end for

infile.close();

displayBag(bagPtr);

std::cout << "isEmpty: returns " << bagPtr->isEmpty() << "; should be 0 (false)" << std::endl;
std::cout << "getCurrentSize returns : " << bagPtr->getCurrentSize() << "; should be 6" << std::endl;

std::cout << "Enter another entry to add: ";
std::cin >> str;
std::cout << "Try to add another entry: add("" << str << "") returns " << bagPtr->add(str) << std::endl;

} // end bagTester