vendredi 1 juillet 2016

C user input to array used as if statement

I am writing code that is taken from the command line. The user will input lets say for example ./a.out -l 2 4 6 into the command line. The goal here is to loop through the array and see if either '-l' or '-s' appear. If '-l' appears it makes x = 1 , if '-s' x = 2 if neither x = 0. Right now the issue being thrown is comparison between pointer and integer on line 7 and 12. Also a multi-character character constant on line 12, which I'm not sure why its being thrown when line 9 is okay. How would I change my if statements to fix the issues being thrown? My code is as follows:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

  int x;

  for(; *argv != '\0'; argv++){
    if(*argv == '-l'){
      x = 1;
  }
    else if(*argv == '-s'){
      x = 2; 
  } 
    else{
     x = 0;
  }
}
  printf("%d",x);
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire