comparing an array of strings to an input string

I have an array of strings like, letters[] = {"a","b"...
i have an input string that I want to do a sanity check on, say "microsoft"
I have a non working subroutine called
void setcompanyName (string mycompanyName) {

for (i=0; i<count;i++){
for( j=0; j<52; j++){
if (data[i] == letters[j]){
companyname = mycompanyName;            
}else {
cout<<"you have entered an incorrect name, setting to: \n";
}
}
}      
}
the code should loop through the word microsoft, should loop through the letter array and do a comparison to make sure no numeric or symbols were entered.  Good advice is grealy appreciated
police45sAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

itsmeandnobodyelseCommented:
>> count, letters, mycompanyname

What's the definition of these variables? I'am assuming the following:

  int count = strlen(mycompynname);

  char* letters[52] = { "a", .... , "z", "A", ....., "Z" }

  char mycompanyname[32];

Yes? Look at the corrected function:

bool MyClass::setcompanyName (string mycompanyName)
{
    int   count     = strlen(mycompanyName);
    // define letters as char and not as char* (strings)
    char  letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (i=0; i<count;i++)
    {
        for( j=0; j<52; j++)
        {
            if (data[i] == letters[j])
                break;  // we break the j loop if we found a valid character
        }  
            if (j >= 52)
            {
                cout<<"you have entered an incorrect name, setting to: \n"
                return false;
            }
    }
    m_companyName = mycompanyName; // assuming class member
    return true;
}

Regards, Alex
0
itsmeandnobodyelseCommented:
Correction: while posting i saw that mycompanyName is a string

Then, the solution turns to

bool MyClass::setcompanyName (string mycompanyName)
{
    int   count     = mycompanyName.length();
    // define all letters as string and not as char*
    string  letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (i=0; i<count;i++)
    {
        for( j=0; j<52; j++)
        {
            if (data[i] == letters[j])
                break;  // we break the j loop if we found a valid character
        }  
        if (j >= 52)
        {
            cout<<"you have entered an incorrect name, setting to: \n"
            return false;
        }
    }
    m_companyName = mycompanyName; // assuming class member
    return true;
}

Regards, Alex
0
brettmjohnsonCommented:
Seems like a job for isalpha().

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rstaveleyCommented:
Brett's right about isalpha being a likely candidate for the job... but if you want to run a test with ranges of characters described by an arbitrary string, you can use basic_string's find_first_not_of.

Here's an absurdly over-engineered illustration using a "valid characters" string generated using the generate_n algorithm. Depending on your application, you might need to consider the validity of numeric characters and symbols like '&'.

--------8<--------
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
#include <iterator>

template<typename T> class PostIncrementGen {
      T t;
public:
      PostIncrementGen(T t) : t(t) {}
      T operator()() {
            return t++;
      }
};

namespace {

      // Valid characters - deployed as a function to allow static initialisation on first call
      const std::string& valid_characters()
      {
            // Valid character set
            static std::string match;

            // Needs to be generated
            if (match.empty()) {

                  // A-Z
                  generate_n(std::insert_iterator<std::string>(match,match.end()),'Z'-'A'+1,PostIncrementGen<char>('A'));

                  // a-z
                  generate_n(std::insert_iterator<std::string>(match,match.end()),'z'-'a'+1,PostIncrementGen<char>('a'));

                  // Space character also allowed
                  match += " ";
            }
            return match;
      }

}

// See if the spacified string is valid
bool valid(const std::string& str)
{
      return str.find_first_not_of(valid_characters()) == std::string::npos;
}

int main()
{
      {
            std::string str = "McAlpine Construction";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "ABC123 Taxi Cabs";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "EZ Cleaning";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "Jones & Co";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }

}
--------8<--------
0
grg99Commented:


I'd put the allowed characters into an old char[] and do a strchr():

char GoodChars[]= "abcdef...xyz_";   int i, HasBad;

for( i = 0, HasBad = 0; i < strlen( InputLine ); i++ ) HasBad = HasBad || strchr(  GoodChars, InputLine[i] ) == 0 );

 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.