0

I'm looking for a way to check a string that a user inputs when prompted (let's take it to be a string variable "userinput") from an array of 10 other strings. So far I have:

    while (userinput.empty()) //Check for empty input
{
    cout << "Please enter your identity.\n"; //Identify user
    getline(cin, userinput);

    do //Check to see if user is the same as the string variable "user"
    {
    cout << "This user is either non existent or has access privileges revoked.\n"; //Wrong username!
    cout << "Please re-enter the username.\n";
    getline(cin, userinput);
    }
    while (user != userinput);
}

It can be seen though, that this only works for a single string variable "user". How would I change this for a string array?

The array itself is the following:

string arr[10] = {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8", "Test9", "Test10"};

Please note: I'm not intending to use passwords, only usernames.

Talisman
  • 388
  • 4
  • 19

3 Answers3

3

You can use the built-in count function like so:

do{
    getline(cin, userinput);
}
while(!std::count(arr, arr+10, userinput));

On ideone as well.

This is how your loop should look:

cout << "Please enter your identity.\n"; //Identify user
getline(cin, userinput);
while (!std::count(arr, arr+10, userinput)) //Check to see if user is the same as the string variable "user"
{
    cout << "This user is either non existent or has access privileges revoked.\n"; //Wrong username!
    cout << "Please re-enter the username.\n";
    getline(cin, userinput);
}

You can see it here.

Scis
  • 2,934
  • 3
  • 23
  • 37
  • I'm getting a 'count' is not a member of 'std' - I am using namespace std, if that helps? – Talisman Jul 19 '14 at 08:55
  • 1
    You probably forgot the `#include ` (just click on the ideone link) :) (BTW using `using namespace std;` is [not recommended](http://stackoverflow.com/questions/1452721).) – Scis Jul 19 '14 at 08:55
  • Ahh, that would explain it! Sorry, just getting to grips with C++. Yeah, I know that it's not really a good idea to use it, but I'll change it once I've cracked this :D – Talisman Jul 19 '14 at 09:00
  • @HarryLudlow Glad to help, do tell if you have any further problems with this solution. – Scis Jul 19 '14 at 09:04
  • Interestingly enough, I do! It seems the first time I input a string which is in the array, it denies it, then accepts the exact same input the second time round! (Sorry to be a PITA!) – Talisman Jul 19 '14 at 09:05
  • @HarryLudlow that's because you had a getline before the `do-while` so it did not check the condition. I've pasted a minimal refactoring proposal that works... – Scis Jul 19 '14 at 09:12
  • *doh*! I just realised that as I worked to test the other solution! – Talisman Jul 19 '14 at 09:13
  • @HarryLudlow Glad it works, BTW using standard built-in functions is a better practice, save your typing effort for the interesting parts :) – Scis Jul 19 '14 at 09:15
  • I see. Much thanks to you; I wish I could give two accepted answers but I'll have to select the other. I shall bear your improvements in mind though! – Talisman Jul 19 '14 at 09:21
2

Put the check in a separate function

 bool isValidUserName(const string& input) {
      for(int i = 0; i < 10; ++i) {
          if(input == arr[i]) {
              return true;
          }
      }
      return false;
 }

and use it as condition in the while

 while (!isValidUserName(userinput));
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks for this. It seems to only validate the first element of the array though, and denies any other username after the first? – Talisman Jul 19 '14 at 08:50
  • @HarryLudlow No it compares every element from the array with the given input parameter. – πάντα ῥεῖ Jul 19 '14 at 08:51
  • Strange. It would only accept the Test1 and not Test2 (or any other), but when in the array I swapped them around, only Test 2 worked. – Talisman Jul 19 '14 at 08:53
  • @HarryLudlow I assumed your array is globally defined. How are you actually using the function? – πάντα ῥεῖ Jul 19 '14 at 08:55
  • I've moved the array up to outside main() because when I used the function inside main() it naturally gave an error. They're both now before the line int main() {. – Talisman Jul 19 '14 at 08:58
  • @HarryLudlow Then I don't see a reason why it shouldn't work, unless you (or me) have made a typo somewhere (can't actually spot one from my side). – πάντα ῥεῖ Jul 19 '14 at 09:00
  • @HarryLudlow I've made up a [working sample](http://ideone.com/df2U8z), you problem is probably, you had missed to apply the check for the first input made. – πάντα ῥεῖ Jul 19 '14 at 09:07
1

If you have a big number of strings to compare, then it will be much more efficiently to use a hash map (std::unordered_set) instead of an array. Search in a hash table is much faster then in an array.

unordered_set<string> valid_inputs {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6"};

Then you can check an user input in such a way:

if (valid_inputs.find(user_input) == valid_inputs.end())
  cout << "error";
else
  cout << "success";
Alex Antonov
  • 942
  • 5
  • 9