Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

if argv == string

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

  if (argv[1] == "yes"); // Wrong, compares two pointers
  if (strcmp(argv[1], "yes") == 0); // This compares what the pointers point to
  if (std::string(argv[1]) == "yes"); // Works fine
  if (argv[1] == std::string("yes")); // Works fine

  // Easy-mode    
  std::vector<std::string> args(argv, argv+argc);
  for (size_t i = 1; i < args.size(); ++i) {
      if (args[i] == "yes") {
          // do something
      }
  }

}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #argv #string
ADD COMMENT
Topic
Name
3+9 =