0

I'm new to c++ and I'm making my first project with it. I have an array with some strings in it and I need my code to check if an input is in that array, store it and continue with the code but I don't know how.

Code:

#include <iostream>
using namespace std;


int battleCapacity;
string T1P1;
string pokemonlist[] = {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

int main() {
    cout << "Pokemon Battle Buddy\nHP and Damage Calculator\nBattle Capacity: ";
    cin >> battleCapacity;
    if (battleCapacity <= 6) {
        cout << "Trainer 1; Pokemon 1: ";
        cin >> T1P1;
    } else {
        cout << "Not Working";
    }
}

I know in Python it is

if x in y:
    return

I'm looking for something similar to this. Thanks

Jayden Collis
  • 145
  • 11
  • [How to find out if an item is present in a std::vector?](https://stackoverflow.com/q/571394/9716597) Just change `vec.begin()` to `std::begin(array)` and `vec.end()` to `std::end(array)` for arrays. – L. F. Aug 31 '20 at 02:07
  • Note If you just want to know if something is in an array, and the array is reasonably large, don't use an array. Use a set. – user4581301 Aug 31 '20 at 04:49
  • Instead of using an array, you can use an std::vector for that purpose. – Yashovardhan Singh Aug 31 '20 at 07:44

4 Answers4

2

C++ How do I check if an object is in an array?

This can be done with the following algorithm:

for each element in array
    if element is the one we are searching, then
       return true
return false

This is called a linear search. More general version of this algorithm can iterate over any range and returns iterator to the found element. There is no need to implement this because the standard library has got it covered already: std::find.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • how would i get ```for each element in array``` to search the whole array? Do I have to do ```for x and y and z in array``` or something else? – Jayden Collis Aug 31 '20 at 02:19
  • @JaydenCollis You can use a loop. – eerorika Aug 31 '20 at 02:21
  • can you edit to show that? I dont want to get anything wrong. Being new to c++ ive no clue what the correct syntax is for nests and loops – Jayden Collis Aug 31 '20 at 02:22
  • @JaydenCollis: it sounds like you need a good C++ book. The basic syntax here is usually `for(const auto& item : collection) { /*doStuff*/ }` – Mooing Duck Aug 31 '20 at 02:23
  • @JaydenCollis I think you've started to learn the language in the wrong way. Start here: https://stackoverflow.com/q/388242/2079303 – eerorika Aug 31 '20 at 02:24
1

If You want to add something to the array it is much more prefarable to use vector instead of a regular array, as are of a fixed size.

that said, here is a simple function to find a string in the vector:

bool stringInVector(vector<string> list, string str)
{
    for(int i = 0; i < list.size(); i++)
    {
        if(list[i] == str)return true;
    }
    return false;
}

You could also use the already existing function find as other users had already mentioned.

Here is what the complete code would look like:

#include <iostream>
#include <vector>
using namespace std;


int battleCapacity;
string T1P1;
vector<string> pokemonlist= {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

bool stringInVector(vector<string> list, string str)
{
    for(int i = 0; i < list.size(); i++)
    {
        if(list[i] == str)return true;
    }
    return false;
}

int main() {
    cout << "Pokemon Battle Buddy\nHP and Damage Calculator\nBattle Capacity: ";
    cin >> battleCapacity;
    if (battleCapacity <= 6) {
        cout << "Trainer 1; Pokemon 1: ";
        cin >> T1P1;
        if(stringInVector(pokemonlist, T1P1)) cout << "Exists";
    } else {
        cout << "Not Working";
    }
}
0

I have an array with some strings in it and I need my code to check if an input is in that array

Instead of array you can use vector, then you can use find standard algorithm:

std::vector<std::string> pokemonlist = {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

if(pokemonlist.end() != std::find(pokemonlist.begin(), pokemonlist.end(), "Charmander"))
{
    std::cout << "found Charmander\n";
}
else
{
    // not found
}
artm
  • 17,291
  • 6
  • 38
  • 54
0

If you just want an already implemented function like in Python, eerorika's answer will suffice. However, if you want to understand this process, in the case of an unordered array you would visit each element in the array and if you reach the end without finding it, then it isn't there.

string value = "example";
for (int i = 0; i < arrSize; i++)
  if (arr[i] == value)
    cout << value << " is in the array.\n";
emegona
  • 168
  • 1
  • 12