iterating through an array for condition statement?

Is it possible to make is a condition which iterated through an array? For example you don't want a user to store duplicate numbers into an array so you have an if statement which says:

1
2
 if (userChoice != //iterate through contents of userChoiceArr){
    userChoice = userChoiceArr[next available element in array];} 



or a vector which i assume would have the same answer
Last edited on
If you are trying to decide whether or not to accept an incoming number into a container depending on the presence of this number in the existing container then an array (either C-style or std::array) would not be ideal because arrays have fixed size and if you do decide to accept the incoming number you'd have to increase the size of the container to accommodate the new number. So here's one way of doing this using std::vector:

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
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v { 3, 4, 5, 6,};
	bool fQuit = false;
	while(!fQuit)
    {
        cout<<"Choose an option \n1. Attempt vector number entry: \n2. Exit\n";
        int choice;
        cin>>choice;
		switch (choice)
		{
            case 1:
            {
                cout<<"Enter number for vector entry: \n";
                int number;
                cin>>number;
                if(find(v.begin(), v.end(), number) != v.end())
                {
                    cout<<"Number already exisits in the vector\n";
                }
                else
                {
                    v.push_back(number);
                    cout<<"New number entered successfully\n";
                }
					break;
            }
            case 2:
				cout<<"You've chosen to exit, Goodbye!"<<endl;
				fQuit = true;
				break;
	    default:
				cout<<"Please choose valid option number!"<<endl;
				break;
        }
    }
}



Topic archived. No new replies allowed.