d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Minesweeper C++ Question > Using Command Line Arguments
12Next
Add Reply New Topic New Poll
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 12 2016 02:47pm
Hi jsp ^_^

I'm working on a simple textual minesweeper game in C++, and I'm having a bit of trouble with this part of the assignment.

*****************
Our game will be a little different, since we do not have a graphical user interface for clicking.
You will run the program providing command-line arguments for the number of rows, columns, and mines in any order. Therefore, the number must bepreceded by an option designator, -r for rows, -c for columns, and –m for mines.

I do not understand the argc (counter) or argv (value) arguments that are in int main(int argc, char* argv[]) or how to use them properly.

Minesweeper itself isn't too hard though :lol:



Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 12 2016 03:05pm
argc is the number of arguments + 1 (argc is always 1, even with no arguments) argv contains arguments

If you run a command line program named a.exe like this in CMD:

a.exe --print

argc will have value 2 and argv[1] array position will have "--print" in it (or a pointer to).

This post was edited by Klexmoo on Mar 12 2016 03:06pm
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Mar 12 2016 03:06pm
edit nvm

This post was edited by spt_94 on Mar 12 2016 03:07pm
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 12 2016 03:09pm
Quote (spt_94 @ 12 Mar 2016 22:06)
when you run the program it will look like: ./minesweeper -r 10 -c 10 -m 20

that means there are 10 rows, 10 columns and 20 mines

the int argc will have the integers in there and the char* argv will have the character in there

so int argc would look like: argc[0] -> 10 ; argc[1] -> 10 ; argc[2] -> 20

char* argv would look like: argv[0] -> 'r' ; argv[1] -> 'c' ; argv[2] -> 'm'

and since you can do the command line argument in any order, just match the argc and argv indexes to create your rows, columns, and mines.

Examples:

./minesweeper -r 10 -c 10 -m 20
argc[0] -> 10 ; argc[1] -> 10 ; argc[2] -> 20
argv[0] -> 'r' ; argv[1] -> 'c' ; argv[2] -> 'm'
match argc[0] and argv[0] so argv[0] = 'r' which means argc[0] = 10 = num rows
match argc[1] and argv[1] so argv[1] = 'c' which means argc[1] = 10 = num columns
match argc[2] and argv[2] so argv[2] = 'm' which means argc[2] = 20 = num mines


./minesweeper -c 11 -r 15 -m 50
argc[0] -> 11 ; argc[1] -> 15 ; argc[2] -> 50
argv[0] -> 'c' ; argv[1] -> 'r' ; argv[2] -> 'm'
match argc[0] and argv[0] so argv[0] = 'c' which means argc[0] = 11 = num columns
match argc[1] and argv[1] so argv[1] = 'r' which means argc[1] = 15 = num rows
match argc[2] and argv[2] so argv[2] = 'm' which means argc[2] = 50 = num mines

./minesweeper -r 44 -c 20 -r 15
argc[0] -> 44 ; argc[1] -> 20 ; argc[2] -> 15
argv[0] -> 'm' ; argv[1] -> 'c' ; argv[2] -> 'r'
match argc[0] and argv[0] so argv[0] = 'm' which means argc[0] = 44 = num mines
match argc[1] and argv[1] so argv[1] = 'c' which means argc[1] = 20 = num columns
match argc[2] and argv[2] so argv[2] = 'r' which means argc[2] = 15 = num rows


That is mostly right, except that (afaik) you will not have anything in argv[0] as that is filled by the command prompt with executable directory or something.
Member
Posts: 89,702
Joined: Jul 14 2008
Gold: 165,885.69
Mar 12 2016 03:09pm
That is fantastic you guys. Thank you.

When I get home, I'll probably post again to help me syntax wise to write this part. I'm still bad at syntax :lol:
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Mar 12 2016 03:11pm
Quote (Klexmoo @ Mar 12 2016 03:09pm)
That is mostly right, except that (afaik) you will not have anything in argv[0] as that is filled by the command prompt with executable directory or something.


I am not that experienced with c/c++. Thought the way I was thinking was right.

Would what I have be correct if all indexes were +1?

I am not sure how you match the -r -c -m with the numbers unless the argv was storing arrays like argv[1] = array{'r',10}
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 12 2016 03:24pm
Quote (spt_94 @ 12 Mar 2016 22:11)
I am not that experienced with c/c++. Thought the way I was thinking was right.

Would what I have be correct if all indexes were +1?

I am not sure how you match the -r -c -m with the numbers unless the argv was storing arrays like argv[1] = array{'r',10}


argc is just the number of arguments, not an array, so in your example:

./minesweeper -r 10 -c 10 -m 20
argc[0] -> 10 ; argc[1] -> 10 ; argc[2] -> 20becomesargc = 3
argv[0] -> 'r' ; argv[1] -> 'c' ; argv[2] -> 'm' becomesargv[0] = (some black magic) ; argv[1] = -r 10 ; argv[2] = -c 10 ; argv[3] = -m 20 (if we ignore the whitespace)

This post was edited by Klexmoo on Mar 12 2016 03:28pm
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Mar 12 2016 03:25pm
Quote (Klexmoo @ Mar 12 2016 03:24pm)
argc is just the number of arguments, not an array, so in your example:

./minesweeper -r 10 -c 10 -m 20
argc[0] -> 10 ; argc[1] -> 10 ; argc[2] -> 20 becomes argc = 3
argv[0] -> 'r' ; argv[1] -> 'c' ; argv[2] -> 'm' becomes argv[0] = (some black magic) ; argv[1] = -r 10 ; argv[2] = -c 10 ; argv[3] = -m 20 (if you ignore the whitespace)


right so you would have to split the string -r 10 and put 10 = num rows.

okay.
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 12 2016 03:25pm
Quote (spt_94 @ 12 Mar 2016 22:25)
right so you would have to split the string -r 10 and put 10 = num rows.

okay.


that is correct :D
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Mar 12 2016 03:27pm
Quote (Klexmoo @ Mar 12 2016 03:25pm)
that is correct :D


thanks, ill repost my examples for the op then.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll