If I have a char array that that looks like
char array[] = {'a', 'b', 'c'};
How can I check user input against all of these values at once?
1 2 3 4
|
if ( user_value == array[0] || user_value == array[1] || user_value == array[2])
{
Do action;
}
|
Last edited on
Is there a way to do it if there were many more elements?
It tells me begin is not a member of the standard library, what's likely the cause of this? Using dev-c++
Edit: Ran it in visual studio c++ and it worked, I don't know why that is.
Last edited on
This equivalent code would work with a somewhat old compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <algorithm>
int main()
{
const char array[] = { 'a', 'b', 'c', '.', 'd', 'e', 'f', 'g', '?' } ;
const char* const end = array + sizeof(array) ; // sizeof(char) == 1
char input ;
std::cin >> input ;
if( std::find( array, end, input ) != end )
std::cout << "input is one of the characters in array\n" ;
}
|
Last edited on