1

I want to use a string array in an if statement to test whether the input string matches any of the strings in the array.

So far this is what I've tried:

void checkForError(char input[50])
{

    const char *input2[]={"print","loadStarter","terminate()"};

    if(input != input2)
    {
        printf("Error:Incorrect method '%s'.\n",input);
    }
    else
    {
        abort();
    }
}

And if I were to enter something in the array like "print" it would end up showing me:

Error:Incorrect method 'print'.

but when I try something not listed in the array like "g" it repeats the error message nonstop.

I was thinking perhaps something like this could work:

void checkForError(char input)
{
  if(strcmp(input,"print"))!=0 || strcmp(input,"loadStarter"))!=0 || strcmp(input,"terminate()")
  {
    printf("Error:Incorrect method '%s'.\n");
  }
  else
  {
    abort();
  }
}

But it turns out that actually doesn't work so what do I do?

1
  • "It repeats the error message nonstop". Impossible from only what you've shown; there is no loop in the code. Show the part where you call checkForError as well, please. Commented Nov 27, 2011 at 6:12

4 Answers 4

4

You cannot compare strings (usefully) in C using == and !=; you must use library functions such as strcmp instead. See How to compare strings in an "if" statement? for details.

Sign up to request clarification or add additional context in comments.

Comments

2

I think a good solution to your question would be to loop around your array, aborting on the first match.

void checkForError(char* input)
{
   const char *input2[]={"print","loadStarter","terminate()"};
   const int len = sizeof(input2)/sizeof(input2[0]);

   for (int i = 0; i < len ;i ++)
   {
       if (strcmp(input,input2[i]) == 0)
       {
          //I have matched the string input2[i]
          abort();
       }
    }

     // Nothing matched
     printf("Not found\n");
 }

This would also be easier to extend than any handcoded method.

Also, if you plan on doing a lot with these strings, and you have a limited number of strings, you should probably turn them into some sort of enum. This way you do not have to have strcmp scattered everywhere, and you can use a standard switch statement.

1 Comment

must be const int len = sizeof(input2)/sizeof(input2[0]);
0

a better method would be to have a return value then have your error message depend on return value.

// return 1 when string is OK, 0 otherwise:

int checkForError(const char* input)
{
  if(!strcmp(input,"print")) || !strcmp(input,"loadStarter")0 || !strcmp(input,"terminate()")
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

Comments

0

Your second thought is correct, you should not compare the strings using == operator, anyway, I'm not sure whether the rest is a typo or not, but it should be like that:

void checkForError(char * input)
//                      ^ note the * (pointer)
{
  if(strcmp(input,"print")!=0 || strcmp(input,"loadStarter")!=0 || strcmp(input,"terminate()") != 0)
//  you forgot brackets
  {
    printf("Error:Incorrect method '%s'.\n", input);
    //                                       ^ you forgot the "input" parameter
  }
  else
  {
    abort();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.