| | |
Problems parsing arguments.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; bool help = 0; bool expl = 0; int main(int argc, char *argv[]) { for(int i=1; i + 1 <= argc; i++) { if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1) { cout << "This is an experiment for parsing arguments.\n" << "Possible arguments are:\n" << "-h : Show this help.\n" << "-e : a short explanation.\n"; help = 1; } else if((argv[i] == "e" | argv[i] == "--expl" ) && expl != 1) { cout << "This program uses a for loop to get i variable value.\n" << "The i variable is used with an if loop to check for arguments.\n"; expl = 1; } else { cout << "Invalid argument: " << argv[i] << "\n"; } } return 0; }
My problem is that I always get the "Invalid argument" message. Any hints?
int universe;
•
•
Join Date: Mar 2008
Posts: 1,486
Reputation:
Solved Threads: 123
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:
Should be:
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.
Line 11:
C++ Syntax (Toggle Plain Text)
if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)
C++ Syntax (Toggle Plain Text)
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 :)
Shouldnt You Be Using "||" For OR.
I mean
Should Be Actually
Right.
I mean
C++ Syntax (Toggle Plain Text)
if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)
Should Be Actually
C++ Syntax (Toggle Plain Text)
if((argv[i] == "-h" || argv[i] == "--help" ) && help != 1)
•
•
Join Date: Mar 2008
Posts: 1,486
Reputation:
Solved Threads: 123
Good point!, he got me thicking strangely. But you cannot compare two arrays using the == operator.
So the correct way would be:
So the correct way would be:
C++ Syntax (Toggle Plain Text)
if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0)&&help != 1)
I need pageviews! most fun profile ever :)
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help writing a simple Ymodem Send
- Next Thread: what should i do first???
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop developer directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






