Need this in c++ please and thank you the instructions are below Use the Concept
ID: 3743733 • Letter: N
Question
Need this in c++ please and thank you the instructions are belowUse 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: AddHello4 Remove5 Print 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: AddStudent0 AddKid0 AddFinal1 AddGrow1 AddNote2 Print AddRich5 Remove1 Remove7 AddMind2 Remove3 Print
Need this in c++ please and thank you the instructions are below
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: AddHello4 Remove5 Print 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: AddStudent0 AddKid0 AddFinal1 AddGrow1 AddNote2 Print AddRich5 Remove1 Remove7 AddMind2 Remove3 Print
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: AddHello4 Remove5 Print 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: AddStudent0 AddKid0 AddFinal1 AddGrow1 AddNote2 Print AddRich5 Remove1 Remove7 AddMind2 Remove3 Print
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: AddHello4 Remove5 Print 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: AddStudent0 AddKid0 AddFinal1 AddGrow1 AddNote2 Print AddRich5 Remove1 Remove7 AddMind2 Remove3 Print
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
Please note that I have updated your Transactions.txt, so that contents in each line are divided by space. Currently, it appears that there is no separator. (AddStudent0 is updated to be Add Student 0)
//code
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main(){
//file name
char filename[]="Transactions.txt";
//opening a file for reading
ifstream inFile(filename);
//checking if file opened successfully or not
if(!inFile){
//file couldnt be opened
cout<<"File not found"<<endl;
return 0;
}
//defining a vector
vector<string> Vec;
string command, name;
int index;
//loop through all contents in the file
while(inFile>>command){
//checking command
if(command=="Add"){
//fetching name and index
inFile>>name>>index;
//validating index
if(index>=0 && index<=Vec.size()){
//adding the string in specified index
Vec.insert(Vec.begin()+index,name);
}
}else if(command=="Remove"){
//getting index of value to be removed
inFile>>index;
//validating index
if(index>=0 && index<Vec.size()){
//removing element at index
Vec.erase(Vec.begin()+index);
}
}else if(command=="Print"){
//printing all elements in the vector
for(int i=0;i<Vec.size();i++){
cout<<Vec[i]<<" ";
}
cout<<endl;
}
}
}
/*Transactions.txt*/
Add Student 0
Add Kid 0
Add Final 1
Add Grow 1
Add Note 2
Add Rich 5
Remove 1
Remove 7
Add Mind 2
Remove 3
/*OUTPUT*/
Kid Grow Note Final Student
Kid Note Mind Student Rich
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.