Problems parsing arguments.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 11
Reputation: FreeFull is an unknown quantity at this point 
Solved Threads: 0
FreeFull FreeFull is offline Offline
Newbie Poster

Problems parsing arguments.

 
0
  #1
Jun 12th, 2008
  1. #include <iostream>
  2. using std::cout;
  3.  
  4. bool help = 0;
  5. bool expl = 0;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. for(int i=1; i + 1 <= argc; i++)
  10. {
  11. if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)
  12. {
  13. cout << "This is an experiment for parsing arguments.\n"
  14. << "Possible arguments are:\n"
  15. << "-h : Show this help.\n"
  16. << "-e : a short explanation.\n";
  17. help = 1;
  18. }
  19. else if((argv[i] == "e" | argv[i] == "--expl" ) && expl != 1)
  20. {
  21. cout << "This program uses a for loop to get i variable value.\n"
  22. << "The i variable is used with an if loop to check for arguments.\n";
  23. expl = 1;
  24. }
  25. else
  26. {
  27. cout << "Invalid argument: " << argv[i] << "\n";
  28. }
  29. }
  30. return 0;
  31. }

My problem is that I always get the "Invalid argument" message. Any hints?
int universe;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,486
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Problems parsing arguments.

 
1
  #2
Jun 12th, 2008
On line 11 and 19 you cannot compare a char string using the == operator. Instead use strcmp http://www.cplusplus.com/reference/c...ng/strcmp.html.

Line 11:
  1. if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)
Should be:
  1. if ((strcmp(argv[i], "-h") == 0) | (strcmp(argv[i], "--help") == 0)&&help != 1)

Do the same thing with line 19 and see if it works then.

You could also use std::string and then use the == operator to compare it with another string.
Last edited by William Hemsworth; Jun 12th, 2008 at 7:15 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: FreeFull is an unknown quantity at this point 
Solved Threads: 0
FreeFull FreeFull is offline Offline
Newbie Poster

Re: Problems parsing arguments.

 
0
  #3
Jun 13th, 2008
Thank you for stopping to help me.
int universe;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Problems parsing arguments.

 
0
  #4
Jun 13th, 2008
Shouldnt You Be Using "||" For OR.

I mean

  1. if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)

Should Be Actually

  1. if((argv[i] == "-h" || argv[i] == "--help" ) && help != 1)
Right.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,486
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Problems parsing arguments.

 
0
  #5
Jun 13th, 2008
Good point!, he got me thicking strangely. But you cannot compare two arrays using the == operator.
So the correct way would be:
  1. if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0)&&help != 1)
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC