Command line arguments?

Hello

I created a guessing game program, and now I have to add a command line argument to it - in this case, if the user runs the program and adds "-n 150" to the end the program will randomly pick a number between 150 and 1 as opposed to the default 100.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
;

int main(int argc, char *argv[])
{
cout << argv[1] <<endl;

if (argv[1] == "-n")
{
	//convert string into integer
	int i;
	sscanf (argv[2], "%d", &i);
	srand (time(NULL));
	int x, y = rand() % i;
	
	cout << "I'm thinking of a number between 0 and 100. Can you guess it?" << endl;

	do
	{
		cin >> x;
		if (x > y)
			cout << "I'm thinking of a smaller number. Please try again." << endl;
		else
		if (x < y)
			cout << "I'm thinking of a larger number. Please try again." << endl;
	}
	while (x != y);

	cout << "Correct! You guessed the secret number!" << endl;
	return 0;
}
else;

	int i;
	srand (time(NULL));
	int x, y = rand() % 101;
	
	cout << "I'm thinking of a number between 0 and 100. Can you guess it?" << endl;

	do
	{
		cin >> x;
		if (x > y)
			cout << "I'm thinking of a smaller number. Please try again." << endl;
		else
		if (x < y)
			cout << "I'm thinking of a larger number. Please try again." << endl;
	}
	while (x != y);

	cout << "Correct! You guessed the secret number!" << endl;
	return 0;
}


The problem, unfortunately, is that I'm not getting anywhere with this command line argument: am I doing something wrong with if (argv[1] == "-n")?
Last edited on
Yes. argv[i] is a char*, not a std::string. To compare them you need to use strcmp(): http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Notice that strcmp() returns 0 (which evaluates to false in a boolean context) if the strings match, which might be a bit counter intuitive.
I'm still not too sure how to do this. If I write

if (strcmp ((argv[1], "-n") == 0)

I get

error: cannot convert 'bool' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
You're trying to pass a bool to strcmp. Try this instead:

if(strcmp(arg[1], "-n") == 0)
A too many parentheses: if (strcmp ((argv[1], "-n") == 0) -> if (strcmp (argv[1], "-n") == 0)
Won't this crash if the user starts it without an argument to argv[1]? The OP should check it against NULL before doing any of this...
IMO the OP should check argc instead of checking each individual argument.

EDIT: and I'd guess that referring to argv[i] when no argument was passed is likely to be an out of bounds error rather than just a null pointer.
Last edited on
Sorry I meant he should have if(argv[1] == NULL) in his code to see if it's blank. Bad terminology on my part.
Er, that won't work, and it tests on the wrong thing. [edit] Hey, that's not what I saw a moment ago (am I seeing things?) ... but it still tests on the wrong thing.

Just convert to std::string to do comparisons:

1
2
#include <iostream>
#include <string> 
1
2
3
4
5
	if ((argc > 2) // because it is PROGNAME "-n" NUMBER
	&& (string( argv[ 1 ] ) == "-n"))
	{
		//stuff goes here 
	}

BTW, you can avoid a lot of code duplication by just getting the number before the game starts.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int complain( const string& progname )
{
	cerr << "Hey, program arguments must be \"-n\" followed by a non-zero, positive integer, as in:\n\n  "
	     << progname << " -n 150\n\n"
	     << "The default is 100.\n";
	return 1;
}

void play( unsigned range )
{
	srand (time(NULL));

	// the game goes here, with random number generation as:
	number_to_guess = rand() % (range + 1);  // for 0..range inclusive
	// or
	number_to_guess = (rand() % range) + 1;  // for 1..range inclusive
}

int main(int argc, char *argv[])
{
	unsigned range = 100;

	if (argc > 2)
	{
		if (string (argv[1]) != "-n")
		{
			return complain (argv[0]);
		}

		istringstream ss (argv[2]);
		ss >> range;

		if (ss.fail())
		{
			return complain (argv[0]);
		}
	}

	play (range);

	return 0;
}

Hope this helps.
Last edited on
Topic archived. No new replies allowed.