My program doesnt like command line variables

I have a program that is supposed to take user input for the command line but my program isnt working correctly. Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char* argv[])
{
	//checks to see if user wants to delete their keys
	if(argv[1] == "-d")
	{
		cout << "Are you sure you want to delete all of your saved keys? "; 
		char choice2;
		cin >> choice2;
		if(choice2 == 'y')
		{
			cout << "Deleting File..." << endl;
			system("del serial.txt");
		}
	}


This is for a serial key storage program that i made. It is supposed to ask the user if they want to delete stored keys but it doesnt work when a '-d' is entered after the program name. Without anything in argv[1] though the program crashes. But the '-d' after the name doesnt to anything the program skips this block of code. How do I fix this?
You are not comparing properly.

== compares addresses.

Use instead:
 
  if (strcmp( argv[1], "-d" ) == 0)


Hope this helps.
that works if '-d' is after but when there is nothing entered the program crashes. How do I fix that? Do I have to check if there is a null in argv[1]?
Oh, sorry.

You can't access parts of an array that don't exist. 'argc' is the number of elements in 'argv'.
 
   if ((argc >= 2) and (strcmp( argv[1], "-d" ) == 0))


Good luck.
Okay thanks, I will try that. Don't you use && instead of and? And could you explain to me why the '(strcmp(argv[1], "-d" ) == 0) has to equal zero.
> Don't you use && instead of and?

In C++ you can use either.

> And could you explain to me why...

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

:-)
Okay I didnt know about the and thing. After closer examination why does the strcmp comparison have to equal zero. In the example you linked to it shows that the 2 strings would return a 1. Does 'strcmp' return a 0 if the conditional is true in C++ or how does that exactly work?
The "and" logical operator is not part of standard C++ though some compilers accept it.
The standard logical operator for and is &&

Good luck :)
"and" is a keyword in standard C++.

Section 2.5.2 says "...each alternative token behaves the same, respectively, as its primary token, except for its spelling."

Table 2 follows and includes "and" as an alternative token for the primary token "&&".
^^ Okay thanks I have always used && but can anyone explain my other question?
Check out cplusplus.com's own strcmp docs: http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

A lot of C functions return ints and because C doesn't/didn't have a boolean type this can sometimes mean int or boolean depending on the function. If it means a boolean value then the normal conventions usually apply (i.e. 0=false otherwise true) however, if it really, really, means int, then the possible return values will depend on the particular function.

In the case of strcmp, 0, helpfully, means strings are equal. Basically, you need to check the docs. ;-)
Last edited on
Okay thankyou.
Topic archived. No new replies allowed.