Need help in C++, i already have the code but how i use void in program Use the
ID: 3747021 • Letter: N
Question
Need help in C++, i already have the code but how i use void in program
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
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
AddRich5
Remove1
Remove7
AddMind2
Remove3
My code below
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
char filename[] = "Transaction.txt";//variable for my file will be filename
ifstream inFile(filename);
if(!inFile) //if file is found & opens or not
{
cout<<"File not found"<<endl;//file did not open or was found
return 0;
}
vector <string> Vec;//defining my vector
string command;
string name;
int index;
while(inFile >> command)//well loop through all contents in the file
{
if(command == "Add")//checking command
{
inFile >> name >> index;//grabbing name and index
if(index >= 0 && index <= Vec.size())//verifying index
{
Vec.insert(Vec.begin() + index, name);//add the string in the specified index
}
else
{
cout << "Wrong index." << endl;
}
}
else if(command == "Remove")//getting index of value to be removed
{
inFile >> index;
if(index >= 0 && index < Vec.size())//checking index
{
Vec.erase(Vec.begin() + index);//removing element at index
}
else {
cout << "Can not remove 7" << endl;
}
}
else if(command == "Print")
{
for(int i=0; i < Vec.size(); i++)//printing all elements in the vector
{
cout << Vec[i] <<" ";
}
cout << endl;
}
}
}
Explanation / Answer
CODE
==================
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
void insert(vector<string>& Vec, string name, int index) {
if(index >= 0 && index <= Vec.size())//verifying index
{
Vec.insert(Vec.begin() + index, name);//add the string in the specified index
} else {
cout << "Wrong index." << endl;
}
}
void remove(vector<string>& Vec, int index) {
if(index >= 0 && index < Vec.size())//checking index
{
Vec.erase(Vec.begin() + index);//removing element at index
} else {
cout << "Can not remove element from the index " << index << endl;
}
}
void print(vector<string>& Vec) {
for(int i=0; i < Vec.size(); i++)//printing all elements in the vector
{
cout << Vec[i] <<" ";
}
cout << endl;
}
int main() {
char filename[] = "Transaction.txt";//variable for my file will be filename
ifstream inFile(filename);
if(!inFile) //if file is found & opens or not
{
cout<<"File not found"<<endl;//file did not open or was found
return 0;
}
vector <string> Vec;//defining my vector
string command;
string name;
int index;
while(inFile >> command)//well loop through all contents in the file
{
if(command == "Add")//checking command
{
inFile >> name >> index;//grabbing name and index
insert(Vec, name, index);
}
else if(command == "Remove")//getting index of value to be removed
{
inFile >> index;
remove(Vec, index);
}
else if(command == "Print")
{
print(Vec);
}
}
}
NOTE: In all these functions, vector<string>& Vec is used to pass the vector by reference.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.