Vector and command

Write a program that creates a vector of strings called V. Vector V grows and shrinks as the user processes the transactions from a data file called “data.txt”. The transaction file can only include three commands: Insert, Delete and Print. With Insert command, you get two more information, the information you need to insert and the position it should be inserted. With Delete, 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:
Insert Hello 4
Delete 5
Print

The first line indicates that The word “Hello” should be inserted in V[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 V[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:

Insert Total 0
Insert Normal 0
Insert Perfect 1
Insert Program 1
Insert Book 2
Print
Insert Perfect 5
Delete 1
Delete 7
Insert Money 2
Delete 3
Print

Note: Each command must be implemented in a separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void insert( string, string, int);

int main()
{

  vector <string>v;
  string operation, value;
  int index;

  ifstream inputFile;
  inputFile.open("data3.txt");

  if(!inputFile)
    {
      cout << "ERROR";
    }
  else
    {
      while (inputFile >> operation >> value>> index)
        {
          v.push_back("operation");
          v.push_back("value");
          v.push_back("index");
        }
      if ((operation >= value) && (value <= operation))
        {
          cout<< " there is imore";
        }
    }
  inputFile.close();

  return 0;
}

  void insert( string operation, string value, int index)
  {


  }




The part I do not understand is : The transaction file can only include three commands: Insert, Delete and Print. With Insert command, you get two more information, the information you need to insert and the position it should be inserted. With Delete, you get one more information that indicates which element (index) should be deleted.

how do i do that?
I know my prototype and my function is wrong I need guidance there as well. I looked in my book but i can't find any string vectors with commands. please help.
Last edited on
You read one word from the file.
* If it is "Insert", then you read another word and a number
* If it is "Delete", then you read a number
* If it is "Print", you don't read additional data
Repeat from start.

Only the values are stored in V.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void insert( string, string, int);

int main()
{

  vector <string>v;
  string operation, value;
  int index;

  ifstream inputFile;
  inputFile.open("data3.txt");

  if(!inputFile)
    {
      cout << "ERROR";
    }
  else
    {
      while (inputFile >> operation >> value>> index)
        {
          v.push_back("operation");
          v.push_back("value");
          v.push_back("index");
        }
      if (insert)
        {
          insert (operation,value, index);
        }

    }
  inputFile.close();

  return 0;
}

void insert( string operation, string value, int index)
{


}

Is this what you mean? and then I just need to write my codes in my void function?

also how do I know if I put my void insert( string, string, int); to constant?
while (inputFile >> operation >> value>> index) ... will not work because the format of the lines are non-repeating. Instead read the file line by line - and use the fact that a line with 0 whitespace would have the Print command, 1 whitespace - Delete, 2 whitespaces - Insert along the following lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <cctype>
#include <algorithm>

int spaceCounter(const std::string& line)
{
    int counter = 0;
    for (const auto& elem : line)
    {
        isspace(elem)? ++counter : counter;
    }
    return counter;
}
int main()
{
    std::vector<std::string> V{};
    std::ifstream inputFile("D:\\input1.txt");
    if(inputFile)
    {
        std::string line;
        while (getline(inputFile, line))
        {
            if (spaceCounter(line) == 0)//Print
            {
                //Print function
            }
            if (spaceCounter(line) == 1)//Delete
            {
                //istringstream line into string and int
                //Delete function - use erase-remove idiom
                //http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector
            }
            if (spaceCounter(line) == 2)//Insert
            {
                //istringstream line into string, string, int
                //Insert function
                //check inputFile is still open before actual insert into V
            }
        }
    }
}


One thing that is not super clear in your instructions:
Write a program that creates a vector of strings called V

Given the first line of the inputFile it seems V should be regarded as an empty vector but it is not clearly stated thus


This could be what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  vector <string> V;
  string operation;
  ifstream inputFile("data3.txt");

  while ( inputFile >> operation ) {
    if ( "Print" == operation ) {
     foo( V );
    }
    else if ( "Delete" == operation ) {
     bar( inputFile, V );
    }
    else if ( "Insert" == operation ) {
     gaz( inputFile, V );
    }
  }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void Insert(vector<string>& v,  string, int);
void Delete(vector<string>& v, int);
void Print(const vector<string>& v);

int main()
{

  vector <string>v;
  string operation, value;
  int index;

  ifstream inputFile;
  inputFile.open("data3.txt");

  if(!inputFile)
    {
      cout << "ERROR";
    }
  else
    {
      while (inputFile >> operation)
        {
      if (operation == "Insert")
        {
          inputFile >>value >> index;

          Insert(v, value, index);
        }
      else if (operation == "Delete")
        {
          inputFile >> index;

          Delete(v, index);
        }
      else if (operation == "Print")
        {
          Print(v);
        }
        }
      inputFile.close();
    }
  return 0;
}

void Insert( vector<string>& v, string value, int index)
{

  if (v.empty())
    {
      v.push_back(value);
    }
  else if(index <v.size())
    {
      v.insert(v.begin() +index, value);
    }

}
void Delete(vector<string>& v, int index)
{
  if (index<v.size())
    {
      v.erase(v.begin()+ index);
    }
}
void Print(const vector<string> &v)
{
  cout << endl;
  for(int i=0; i< v.size(); i++)
    {
      cout << v[i] << " ";
    }
  cout << endl;
  cout << endl;
}




Thanks guys ! This is what worked.
Last edited on
Thanks guys ! This is what worked.

I don't think Insert is quite right. With the code above any index can be used to insert at on an empty container.

Should be sufficient:
1
2
3
4
5
6
7
void Insert( vector<string>& v, string value, int index)
{
    if (index <= v.size())
    {
        v.insert(v.begin() + index, value);
    }
}
Topic archived. No new replies allowed.