-1

I have a string array with multiple names. I would like to check using an if statement if the user input is equal to any of the names.

For example:

names[5] = {'david','rares','tudor','john','jay'}

cin>>name;

And now i would like to check if name is equal to any of the elements in the array.

Very Bad Example:

if(name == names) 
{
cout<<"you can use this name. Name: "<<name;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Hi! The easier way to do that would be using a `for` loop and check each of those elements :) – Esdras Xavier Apr 11 '19 at 20:29
  • 2
    `'david'` is a multibyte character, not a string. The compiler may be warning you about this. Use `"david"` instead. Also watch out for case. `"david"` will not match `"David"` – user4581301 Apr 11 '19 at 20:29
  • [`std::find`](https://en.cppreference.com/w/cpp/algorithm/find) is useful for finding one value in a list. – BoBTFish Apr 11 '19 at 20:30

3 Answers3

0

As pointed out by user4581301, 'david' isn't a string but a multibyte character (and most probably not a valid one either). If you want to keep a set of names and check if some name exists in it, you could use a std::set or std::unordered_set instead of an array. Example:

#include <iostream>
#include <set>
#include <string>

int main() {
    std::set<std::string> names = {"david", "rares", "tudor", "john", "jay"};
    std::string name;

    std::cin >> name;
    if(names.count(name)) // or names.contains(name) in C++20
        std::cout << "found\n";
    else
        std::cout << "not found\n";
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Is there a way without using std? – David Militaru Apr 11 '19 at 21:13
  • Sure, you can do `using std::cin, std::cout;` and then skip `std::` on those if you use those a lot. You can even use the whole namespace `std` but that's [not adviceable](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Ted Lyngmo Apr 11 '19 at 21:35
0

You can do something like this:

#include <iostream>
#include <algorithm>

int main()
{
    const char * v [] = { "david", "rares", "tudor", "john", "jay" };
    std::string name;
    std::cin >> name;
    auto result = std::find (std::begin (v), std::end (v), name);
    if (result != std::end (v))
        std::cout << name << " is valid\n";
}

although please note that most people would use std::vector <std::string> to hold the list of strings.

Live demo

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

I would suggest using a vector to store the names so you know your size. If you must use a static array, make sure to save the current size and update it whenever removing/adding names.

Somebody may have a more efficient solution, but you can linearly search through the array using a for loop.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    string names[5] = {"david","rares","tudor","john","jay"};
    cout << "Enter name: ";
    cin >> name;

    for (int i = 0; i < 5; i++)
    {
        if(names[i] == name)
        {
            cout << name << " was found in the array." << endl;
            return 0;
        }
    }

    cout << name << " wasn't found in the array." << endl;
    return 0;
}

This solution won't work for case sensitive names (David and david are considered two seperate names).

borninla
  • 51
  • 11