-3

If I get rid of the function "int check(char inp)" and put its code in "main" the program runs fine. But when I try to make a separate function to check the input I get errors.

#include <stdio.h>
#include <string.h>

/*trying to create a function that handles the "strcmp" functions*/
int check(char inp)
{
  /*int c in this function holds the return value*/
  int c;
  char option1[20] = "cute puppy";
  char option2[20] = "good evening frank";
  char option3[20] = "are those new shoes";
  
  /*checks input against "option" variables for a match*/
  if (strcmp(inp, option1) == 0)
  {
    c = 1;
  }
  else if (strcmp(inp, option2) == 0)
  {
    c = 2;
  }
  else if (strcmp(inp, option3) == 0)
  {
    c = 3;
  }
  return c;
}

int main(void)
{
  /*variable that stores input*/
  char inp[20];



    printf("You begin by walking down the sidewalk just outside your house.\n"
           "Its 11pm, nobody is out except your neighbor who is walking his dog.\n"
           "What do you say as you walk past?\n\n"
           "cute puppy - good evening frank - are those new shoes\n");

  /*gets input from user and places it in "inp"*/
  gets(inp);

  /*if match is found int value is returned and assigned to "c" in "main"*/
  int c = check(inp);

  if (c == 1)
  {
    printf("works1\n");
    return 0;
  }
  else if (c == 2)
  {
    printf("works2\n");
    return 0;
  }
  else if (c == 3)
  {
    printf("works3\n");
    return 0;
  }
 return 0;
}

play.c: In function 'check': play.c:11:14: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion] if (strcmp(inp, option1) == 0)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

2

The function parameter is declared incorrectly. Instead of the type char

int check(char inp);

it shall have the type const char *

int check( const char * inp);

The function invokes undefined behavior in case when the passed string is not equal to any string defined within the function.

The function could be defined the following way

int check( const char *inp )
{
    size_t i = 0;
    
    const char * options[] = 
    { 
        "cute puppy", "good evening frank", "are those new shoes"
    };
    
    const size_t n = sizeof( options ) / sizeof( *options );
    
    while ( i != n && strcmp( options[i], inp ) != 0 ) ++ i;
    
    return i == n ? -1 : i;
}

Pay attention to that the function gets is unsafe and is not supported by the C Standard. Instead use the function fgets. For example

fgets( inp, sizeof( inp ), stdin );
inp[ strcspn( inp, "\n" ) ] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The error is telling you that the first argument you are passing to strcmp on line 11 is an integer and not a pointer. This is happening because char is an integer type in C, and in the definition of your check function on line 5 you specified inp is a char instead of a char* (pointer to a char), so that's what it thinks the type of inp is.

Adam Feor
  • 513
  • 3
  • 10