Thread: Need help for comparing a char to an array char

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    Need help for comparing a char to an array char

    Im trying to compare a single char to an array of chars like for example

    Code:
    char input[100];
    char notValid[20] = {'A','B','C'}
    ..
    cin << input
    
    ..
    
    if(char input[0] == notValid) //
    {
       ..
    }
    basically here im comparing input[0] for each element in the notValid array. but my code instead checks the first element only in the array. How to compare it to all elements in the notValid array?

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Run a for loop and use the loop control variable as the index to the array.

    Code:
    for(int loopcontrol = 0; loopcontrol < arraysize; loopcontrol++)
      if(input[loopcontrol] == stuff)
        return true;
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is unclear what you are trying to achieve.
    But first off, do not ever mix char arrays and std::cin >>. Use std::string instead of char arrays.
    So, what do you want to accomplish? Do you want to see if your inputted strings consists of one or more of the not valid input characters?
    If so, then this should do what you want:

    Code:
    std::string input;
    const std::array<char> notValid({'A','B','C'}); // Requires a recent C++11 compiler.
    const char notValid[] = { 'A', 'B', 'C' }; // Use this if the above does not compile
    
    ...
    
    std::cin >> input;
     
    ...
     
    if (std::find_first_of(input.begin(), input.end(), notValid.begin(), notValid.end()) != input.end()) // Use if your compiler supports C++11
    if (std::find_first_of( input.begin(), input.end(), notValid, notValid + (sizeof(notValid) / sizeof(notValid[0])) ) != input.end()) // Use if you compiler does NOT support C++11
    {
    	// One or more characters in notValid was found in input.
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. Comparing char array for two strings
    By Asbestos in forum C++ Programming
    Replies: 5
    Last Post: 02-16-2006, 12:41 AM
  4. Comparing char values in an array using 'if'
    By Judy_528 in forum C++ Programming
    Replies: 18
    Last Post: 04-25-2002, 07:52 AM
  5. ? comparing elements of a char array
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2001, 07:55 AM