Help! string list

Im confused as how to make a program that compares a users name and checks if it is in a list of names!!!
i made the list but ya im not sure of how to do it
string VIPlist[];
VIPlist [0]= "Miceal";
VIPlist [1]= "Youssef";
VIPlist [2]= "Saif";
VIPlist [3]= "Temkit";
VIPlist [4]= "Karim";
VIPlist [5]= "Sarah";
VIPlist [6]= "Daniel";
VIPlist [7]= "Nelson";
VIPlist [8]= "Kyle";
VIPlist [9]= "David";
All you will need to do is loop through your array and test each element in the array against the username that has been entered and see if any match.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Our username array
string usernames[] = {"Bob", "Joe", "Jill"};

// Get username to test
std::string username = "";
std::cin >> username;

// Loop through the array and test each element
// I am using C++11 features here but it should give you a idea
// even if you aren't familiar with iterators or C++11 features.
// All you need to do is loop through the array and test each element
// it doesn't matter how you accomplish that.
for (auto it = std::begin(usernames); it != std::end(usernames); ++it)
{
    // Test the element to see if it is the same as the username string.
    // if it is handle it accordingly if not continue to the next element.
}


Now somethings to consider is if you want it to return when it hits the first result that compares to true or to return all returns that compare to true. You also need to take into account whether you want to handle the case of the usernames, ect. Just some things to think about after you get it up and running.

All std::begin and std::end do here is return a iterator to the beginning element and one past the last element of the array. It is basically just like std::vector::begin() and std::vector::end().

http://www.cplusplus.com/reference/iterator/begin/
http://www.cplusplus.com/reference/iterator/end/
Last edited on
thanks!!
Topic archived. No new replies allowed.