Error parsing commandline arguments

Hi guys,
This is my first time writing a program that takes arguments. I have read a couple articles on how to get and parse the argument, including one on this website. But the program just wouldn't recognize the flags. what is wrong? I think my program looks exactly same with those in tutorials.

I use VS 2008 on a 64 bit PC. The program was run with arguments "-g GRB2 -n 1000"

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	string geneName;
	size_t testRep;

	if(argc != 5)
	{
		cout << "Not enough arguments"<<endl;
		exit(0);
	}
	else
	{
		for(size_t i = 1; i<5;i++)
		{
			if(argv[i] == "-g")
				geneName = argv[i+1];
			if(argv[i] == "-n")
				testRep = atoi(argv[i+1]);
			else
			{
				cout << "Tag error"<<endl;
			}
		}
	}
	return 0;
}
Last edited on
You can't compare C strings with the == operator.
Convert the parameters to a string vector to fix:

vector<string> args(argv+1,argv+argc);
All done. Thanks Athar!
Topic archived. No new replies allowed.