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

Use the Concept of vector and string to do the following question 1) Write a pro

ID: 3745846 • Letter: U

Question

Use the Concept of vector and string to do the following question 1) Write a program that creates a vector of string called "Vec". Vector "Vec" grows and shrinks as the user processes the transactions from a data file called "Transaction.txt". The transaction file can only include three types of commands: Add, Remove, and Print. With "Add" command, you get two more information, the information you need to put into the vector and the position the information should be inserted. With "Remove", you get one more information that indicates which element (index) should be deleted. Print command means the content of the vector should be printed on the screen. For example, you may have the following information in your file: Add Remove Print Hello 4 The first line indicates that the word "Hello" should be located in the forth index which means Vec [4]. Of course you should check if this insert is possible. This insert is possible if the position you are attempting to insert the element is not beyond the size of the vector. The second line means Vec [5] should be removed. Again this operation should only be allowed if the index is not beyond the current size of the vector. Test your program with the following Transaction file: Add Student 0 Add Kid Add Final Add Grow Add Note Print Add Rich Remove Remove Add Mind Remove Print 0

Explanation / Answer

/*
* File: main.cpp
* Author:
*
* Created on 12 September, 2018
*/

#include <cstdlib>
#include <fstream>
#include<iostream>
#include<sstream>
#include <vector>
using namespace std;

/*
* Creates a vector of string and does operations on it according to file "Transaction.txt"
*/
int main(int argc, char** argv) {
vector<string> Vec;
ifstream input("Transaction.txt");//open file
string line;
string operation,item;
int index;
if(input.is_open())//if file present
{
while(getline(input,line))//read a single line
{
stringstream ss(line);
ss>>operation;//split the line read into words ie default delimiter ' '
switch(operation.at(0))//get first letter of operation
{
case 'A'://add
ss>>item;//read item to insert
ss>>index;//read index at which to insert
if(index<=Vec.size())//if insertion possible at index
{
Vec.insert(Vec.begin()+index,item);//insert
cout<<"ADDED "<<item<<" at "<<index<<". ";
}
else
cout<<"ADD FAILED";
break;
case 'R':
ss>>index; //read index from where remove has to be done   
if(index<Vec.size()) //if element present at index
{
item=Vec.at(index);//get element to remove
Vec.erase(Vec.begin()+index);//remove
cout<<"REMOVED "<<item<<" from "<<index<<". ";
}
else
cout<<"REMOVE FAILED at "<<index<<" ";   
break;
case 'P':
cout << " VECTOR NOW...: "; //print vector
for (int i = 0; i < Vec.size(); i++)
cout <<i<<" "<<Vec[i] << " ";
}
}
input.close();
}
else
cout<<" Cannot open file" ;
return 0;
}

sample output

ADDED student at 0.
ADDED kid at 0.
ADDED final at 1.
ADDED grow at 1.
ADDED note at 2.

VECTOR NOW...:
0 kid
1 grow
2 note
3 final
4 student
ADDED Rich at 5.
REMOVED grow from 1.
REMOVE FAILED at 7
ADDED Mind at 2.
REMOVED final from 3.

VECTOR NOW...:
0 kid
1 note
2 Mind
3 student
4 Rich

RUN SUCCESSFUL (total time: 154ms)