vectors

The program will prompt the user for a value and will try to find it in the populated vector in the main function. The program will call the 'findName' function, which you will write.

If the value is in the vector, then erase it, but return the index it was found in. The 'findName' function MUST return -1 if the value is NOT found in the vector.

If the value is not in the vector, the main will print, "The name is not found."

At the end of the program, the vector will be printed out to check if the correct name was deleted or not.


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
  #include <iostream>
#include <vector>
#include <string>

using namespace std;

void addNames(vector<string>& names)
{
     names.push_back("Seppi");
     names.push_back("Farrell");
     names.push_back("Burton");
}

void print(vector<string> names)
{
     for (int i = 0; i < names.size(); i++)
     {
          cout << names[i] << endl;
     }
}
int main()
{
     vector<string> names;

     addNames(names);

     string nameToErase;
     cin >> nameToErase;

     int indexFound = findName(names, nameToErase);

     if (indexFound != -1)
     {
          cout << indexFound << endl;
     }
     else
     {
          cout << "The name is not found." << endl;
     }

     print(names);

     return 0;
}
Where is your findName function defined?
Topic archived. No new replies allowed.