About vectors ...

Hi. I have a string vector with the following elements: { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }. The question is: "how to write a statement like - if the user input is in the list of vector elements return true, for example if the user enters "four" the program searches for it among vector elements and returns true or false. I don't know should I use find or something like this.

Thank you in advance.

I don't know should I use find or something like this.
Using find seems like a best choice: http://en.cppreference.com/w/cpp/algorithm/find

Look at example at the bottom, it shows how to detect if particular value contained in sequence.
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
83
84
85
86
87
88
89
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

std::vector<std::string> numbers={"one","two","three","four","five","six","seven","eight","nine"};

bool CheckVector(int num)
{
  std::string elem;
  bool stop=false;
  switch(num)
  {
    case 1:
    {
        elem="one";
        break;
    }
    case 2:
    {
        elem="two";
        break;
    }
    case 3:
    {
        elem="three";
        break;
    }
    case 4:
    {
        elem="four";
        break;
    }
    case 5:
    {
        elem="five";
        break;
    }
    case 6:
    {
        elem="six";
        break;
    }
    case 7:
    {
        elem="seven";
        break;
    }
    case 8:
    {
        elem="eight";
        break;
    }
    case 9:
    {
        elem="nine";
        break;
    }
    default:
    {
       elem="fail";
    }
  }
  if(std::find(numbers.begin(), numbers.end(), elem)!=numbers.end())
  {
     return true;
  }
  else
  {
     return false;
  }
}

int main()
{
  int num;
  std::cout << "Input A Number" << std::endl;
  std::cin >> num;
  bool ans=CheckVector(num);
  if(ans==true)
  {
     std::cout << std::endl << "Number Recognized";
  }
  else if(ans==false)
  {
     std::cout << std::endl << "Number Not Recognized";
  }
  return 0;
}

Perhaps that code will work. I haven't tested it but I think it's what you're looking for.
MiiNiPaa, Homberto, thank you very much. I appreciate.
Friends, with your help and some more search in the web, finally I did what I needed. Here is the code:

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<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

int main()
{
	vector <string> values = { "one", "two", "three", "four", "five",
		"six", "seven", "eight", "nine" };
	vector <int> values_n = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	string val = " ";
	cout << "Enter any number from 'one' to 'nine': ";
	cin >> val;
	
	
	for (;;)
	{
		auto result = find(begin(values), end(values), val);
		if (result != end(values))
		{

			cout << val << ": " << values_n[(result - values.begin())] << endl;		
			cout << "Enter any number from 'one' to 'nine': ";
			cin >> val;
		}

		else
		{
			cout << "Error.\n";
			cout << "Enter any number from 'one' to 'nine': ";
			cin >> val;

		}
	}
	
				
	system("PAUSE");
	return 0;

}
Last edited on
Topic archived. No new replies allowed.