Trouble with argv[]

This is a discussion on Trouble with argv[] within the C++ Programming forums, part of the General Programming Boards category; Hello, How could I compare command-line arguments to strings? I've got the code below that works with single characters. What ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    32

    Trouble with argv[]

    Hello,

    How could I compare command-line arguments to strings? I've got the code below that works with single characters. What should I change to make it work with strings?

    Code:
    int main(const int argc, char* argv[]) {
    
      bool v = false;
      for(int i = 1; i < argc; i++) {
        if(*argv[i] == 'v')
          v = true;
      }
    Seron

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use strcmp() for null terminated c style strings.
    for stl strings use member function compare()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    I'm a beginner at C++ and have not come across strcmp() or compare() . Are there any resources on the web to learn more about how these are used?

    Seron

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    strcmp

    strcmp, strcpy, strcat, and many others are part of the c library <string.h>


    If you have any book on C++ or C it should talk about the use of these functions other wise this is how they work...


    strcompare( string1, string2) will return 0 if they are equal < 0 if string1 is less than string 2 or > 0 if string2 is greater than string1.

    e.g.

    char string1[20] = "Mickey";
    char string2[20] = "Pluto";

    if( strcmp(string1, string2) == 0) result is false
    if( strcmp(string1, string2) > 0) result is false
    if( strcmp(string1, string2) < 0) result is true because Mickey < Pluto

    In the case with the argv statements you would compare like this

    if( strcmp(argv[1], "something") == 0)
    ..do something
    else if( strcno( argv[2], "something") == 0)
    ...do something else
    ...
    zMan

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thank you zMan.

    One more question. Why is Mickey < Pluto ?

    Seron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. strcat (), argv[], general string trouble
    By markjoe in forum C Programming
    Replies: 11
    Last Post: 10-30-2007, 04:53 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  4. when i would use char *argv[] and when char** argv[] ?
    By blue_gene in forum C Programming
    Replies: 17
    Last Post: 04-14-2004, 07:00 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 08:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21