Ben
Ben

Reputation: 17

Function not returning 0 due to comparison error

I'm new to programming.

The purpose of this function (validate) is to validate if the user has entered a correct command in the command prompt that being either: -h,-k,-f,-e, or -d. if the user hasn't done this then the program prints an error message returns a boolean zero to the main function and then ends the program. If the user uses one of the commands then instead it returns a 1 and the program continues in the main function.

However I'm getting this error message from the compiler:

comparison between pointer and integer
('bool (*)(char *, char *, int)' and 'int')
if (validate==1){"

This is my function:

bool validate(char* file_check, char* keyfile_check, int argc) {
  int validationcleared;
  if (strcmp(file_check, "-h") != 0 && strcmp(file_check, "-k") != 0 &&
    strcmp(file_check, "-f") != 0 && strcmp(file_check, "-e") != 0 &&
    strcmp(file_check, "-d") != 0) {
    printf("please use a valid file\n");
    return 0;
  }
  else if (strcmp(file_check, "-k") == 0 && (argc < 3)) {
    printf("please use a digit character for keyfile\n");
    return 0;
  }
  else {
    return 1;
  }
}

This is my main function:

int main(int argc, char* argv[]) {
  int validationcleared;
  if (argc < 2) {
    printf("Usage: partb 1 2 3 \n");
    exit(-1);
  }
  char* filename = argv[1];
  char* keyfile = argv[2];

  validate(filename,keyfile, argc);
  if (validate==1) {
    printf("validation test passed\n");

  }

  else {
    printf("validation test not passed\n");
    return 0;
  }
}

Upvotes: 0

Views: 82

Answers (1)

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27632

This is a rather common mistake among beginners:

validate(filename, keyfile, argc);
if (validate == 1) {
    // ....

If you want to check the return value from a function, you should use the actual function call, not the name of the function. Like this:

if (validate(filename, keyfile, argc) == 1) {
    // ....

Or, if you still want to do it in several steps:

bool result = validate(filename, keyfile, argc);
if (result == 1) {
    // ....

Upvotes: 4

Related Questions