Arrays

Hi so this code is supposed to have 8 different inputs, and I want the cin statement to first check if the input is in the array, but I have no idea in how to do this. any suggestion, comments or ideas. thank you so many, you guys are the best

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
 char D1;
 /*char alphabet[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
 char numbers[10] = {'0','1','2','3','4','5','6','7','8','9'};
 */cout<<"enter a letter ";
 cin>>D1;
 if ( char D1 != 'a'  )
  {
      cout<<"not special characters allowed";
  }
  else 
  {
      cout<<"good job";
  }
cin.ignore();
cin.get();
return 0;
}
Here's a way that is simple to understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> input;

bool input_is_in_array = false;

for (int i = 0; i < size_of_array; i++)
{
  if (array[i] == input)
  {
    input_is_in_array = true;
  }
}

// input_is_in_array  will be true here is the input was in the array 



Here's a way to make more use of what C++ offers you, with std::find/
1
2
3
4
if ( std::find(array, array + array_size, input) == (array + array_size))
{
  // input was NOT found in the array
}


Topic archived. No new replies allowed.