Like Tree6Likes

command line arguments

This is a discussion on command line arguments within the C++ Programming forums, part of the General Programming Boards category; I am writing a simple program using command line arguments (int main(int argc, char* argv[]). It must search through a ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    command line arguments

    I am writing a simple program using command line arguments (int main(int argc, char* argv[]). It must search through a file of text for a character and count how many times it appears in the string.

    I have inputted the string and can access all characters of the string (sentence[i]). But I have issues comparing it to the character stored in argv[1], since it is a char*.

    Code:
    for (int j=0; j<counter;j++) // counter = number of char in sentence string
        if (sentence[j] == argv[1])
          count++;
    This is comparing two different types, so it cannot be done. I have tried using .c_str() but not successfully.

    Any guidance would be appreciated. :)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So either convert the char* to a string and use ==, or convert the string to a C-string and use strcmp.
    stefanyco likes this.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Is sentence an std::string? If yes, you can just remove the loop and use
    Code:
    if(sentence == argv[1])
    tabstop likes this.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Quote Originally Posted by _Mike View Post
    Is sentence an std::string? If yes, you can just remove the loop and use
    Code:
    if(sentence == argv[1])
    He can't, he needs to retreive the number of occurences.

    Code:
    std::cout << std::count(sentence.begin(), sentence.end(), argv[1][0]) << std::endl;
    The sentence must be of std::string type obviously.
    tabstop and _Mike like this.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by kmdv View Post
    He can't, he needs to retreive the number of occurences.
    Ah yes you are absolutely correct. I misread the OP and thought he was trying to compare an std::string to a c-string by checking each character one by one.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    I keep getting 0 as the count.

    ./a.out a -i <commandlineinput

    It needs to search for and count how many a's are present in the string that's in the commandlineinput file.

    I need to find a way to compare the char* a to the string to find how many occurences there are.
    Is there a way to convert char* to string? Or vise versa?
    That way i can use strcmp()

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you looking for the word a or the letter a?

    Either way, you can use string::find in a loop to keep searching through your sentence until you don't find any more.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Post complete code
    I never put signature, but I decided to make an exception.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    This is what the code looks like now.

    Code:
    int main(int argc, char* argv[])
    {
      string sentence;
      int counter=0; 
      int count =0;
      while (!cin.eof())
        {
          cin >> sentence[counter];
          counter++;
        }
      for (int i=0; i<counter;i++)
        cout << sentence[i];     //
      cout << counter << endl; // just to double check it reads correctly  
      for (int j=0; j<counter;j++) // counter = number of char in sentence string
          if (sentence[j] == argv[1]) // here is the issue
            count++;
      cout << "Count of " << argv[1] << ": " << endl;
      return 0;
    }
    I will mess around with the code more and try what tapstop suggested.
    :)

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    In the following snippet:
    Code:
    if (sentence[j] == argv[1])
    sentence[j] is a character while argv[1] is a character string. You can not compare a character to a character string. If you want the individual character in argv[1] you would use argv[1][j]. But you also need to insure that j is never larger than strlen(argv[1]).

    Also this line should be causing problems:
    Code:
    cin >> sentence[counter];
    Your sentence variable is a std::string but your input says it should be an array of std::string.

    Where are your include files?


    Jim

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Quote Originally Posted by jimblumberg View Post
    Also this line should be causing problems:
    Code:
    cin >> sentence[counter];
    Your sentence variable is a std::string but your input says it should be an array of std::string.
    I disagree: the expression actually resolves to a char type. You're allowed to fill in the elements of a string just like you are allowed to fill in the elements of an array directly. The only issue is that, if sentence is really a sentence, then the fact that cin>> ignores whitespace is a potential problem.

    I couldn't do a.out " " < commandlineinput

    assuming I'm even invoking the program correctly. I'm not sure.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by jimblumberg View Post
    In the following snippet:
    Code:
    if (sentence[j] == argv[1])
    sentence[j] is a character while argv[1] is a character string. You can not compare a character to a character string. If you want the individual character in argv[1] you would use argv[1][j]. But you also need to insure that j is never larger than strlen(argv[1]).
    Since argv[1] is expected to be a single character, he only needs to dereference argv[1]. Then the code should be:

    Code:
    if (sentence[j] == *argv[1])

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Thank you all :)
    I didn't need to output the string with whitespace, that was just for me to check. I also re-read a chapter on pointers and such and figured it out. Sorry!
    Ushakal is correct, by dereferencing I was able to compare to each character of the string.

    Code:
      while (!cin.eof())
        {
          cin >> sentence[counter];
          if (sentence[counter] == *argv[1])
            count++;
          counter++;
        }
    That works. Thank you! :)
    (*she)

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by stefanyco View Post
    (*she)
    My apologies.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by stefanyco View Post
    Thank you all
    I didn't need to output the string with whitespace, that was just for me to check. I also re-read a chapter on pointers and such and figured it out. Sorry!
    Ushakal is correct, by dereferencing I was able to compare to each character of the string.

    Code:
      while (!cin.eof())
        {
          cin >> sentence[counter];
          if (sentence[counter] == *argv[1])
            count++;
          counter++;
        }
    That works. Thank you!
    (*she)
    Do note that you should first check argc, which you weren't doing in the code you posted in #9, to see that the user has actually written any arguments or else *argv[1] might dereference an invalid pointer.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By BEN10 in forum C Programming
    Replies: 16
    Last Post: 06-04-2009, 05:43 PM
  2. Command line arguments...
    By AeonMoth in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2007, 07:41 AM
  3. command line arguments
    By Mr.Pink in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 04:32 PM
  4. Command line arguments
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2002, 11:59 AM

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