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

Program 2 Write a program that creates an empty vector of strings called V. Vect

ID: 3745527 • Letter: P

Question

Program 2 Write a program that creates an empty vector of strings called V. Vector V grows and shrinks as the user processes commands from a data file called "vectorData.txt". Each line in the transaction file contains a command and the corresponding data. For example, you may have the following information in your file: Insert Delete Print Hello The transaction file can only include three types of commands: Insert, Delete, and Print Insert command inserts a string value in the vector at a specific position. So the Insert command comes with two more information, the string you need to insert and the position it should be inserted at. For example, the first line indicates that the word “Hello" should be inserted in V[4]. You should check if this insert is possible. It is possible if the position you are attempting to insert the element is a positive number not beyond the size of the vector . Delete command deletes the element at the specified position. So Delete comes with one more information that indicates which element (index) should be deleted. For example, The second line means V[5] should be removed. Again this operation should only be allowed if the index is positive and not beyond the current size of the vector Print command prints the contents of the vector on the screen . You may test your program with the following data file: Delete Insert Insert Insert Insert Insert Insert Insert Insert Insert Delete Insert Delete Insert Delete Print Students Welcome Back Ready Get To Program Very Good Job CS211 Sample Output Welcome CS211 Students Very Good Job Note: Each command must be implemented in a separate function

Explanation / Answer

#include<iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

// Function to validate position
// Returns true if valid position otherwise returns false
bool validatePosition(int pos, int len)
{
// Checks if position is greater than or equals to 0 and less than or equals to length returns true
if(pos >= 0 && pos <= len)
return true;
// Otherwise returns false
else
return false;
}// End of function

void insertData(string *arr, string f, int pos)
{
arr[pos] = f;
}// End of function
void deleteData(string *arr, int pos)
{
arr[pos] = "";
}// End of function
void printData(string *arr, int len)
{
for(int x = 0; x < len; x++)
{
if(arr[x] != " ")
cout<<arr[x]<<" ";
}// End of for loop
}// End of function

// Function to read data from file and store in the vector
void readFile(string *arr)
{
// To store counter of commands
int counter = 0;
// To store read from file
int position;
// To store string data read from file
string stringData;

// Creates an object of ifstream
ifstream rFile;

// Opens the file Address.txt for reading
rFile.open("VectorData.txt");

// Checks if file is unable to open
if(!rFile)
{
// Displays error message
cout<<" ERROR: Unable to open the file for reading.";
// Closer program
exit(0);
}// End of if condition

// Loops till end of file
while(!rFile.eof())
{
// Read the data from file
getline(rFile, stringData);
// Increase the counter by one
counter++;
}// End of while

// Close file
rFile.close();

// Dynamically allocates memory of size counter
arr = new string[counter];

// Loops till counter value
for(int x = 0; x < counter; x++)
// Initializes null to each position
arr[x] = " ";

// Re - opens the file for reading
rFile.open("VectorData.txt");

// Loops till end of file
while(!rFile.eof())
{
// Reads the command data
rFile>>stringData;

// Checks if the command is "Insert"
if(stringData.compare("Insert") == 0)
{
// Reads the data
rFile>>stringData;
// Reads the position
rFile>>position;
// Calls the function to validate the position
if(validatePosition(position, counter))
// If valid position calls the function to insert data
insertData(arr, stringData, position);
// Otherwise displays error message
else
cout<<" ERROR: Invalid position ["<<position<<"] to insert!"<<endl;
}// End of if condition

// Otherwise checks if the command is "Delete"
else if(stringData.compare("Delete") == 0)
{
// Reads the position
rFile>>position;
// Calls the function to validate the position
if(validatePosition(position, counter))
// If valid position calls the function to delete data
deleteData(arr, position);
// Otherwise displays error message
else
cout<<" ERROR: Invalid position ["<<position<<"] to delete!"<<endl;
}// End of else if condition

// Otherwise checks if the command is "Print"
else if(stringData.compare("Print") == 0)
// Calls the function to display data
printData(arr, counter);
// Otherwise display invalid command error
else
cout<<" ERROR: Invalid command!";
}// End of while loop
}//End of function

// driver function definition
int main()
{
// Declares a string pointer
string *records;
// Calls the function to read data from file
readFile(records);
}// End of main function

Sample Output:

ERROR: Invalid position [-1] to delete!

ERROR: Invalid position [-1] to insert!
Welcome CS211 Students Very Good Job

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